// Triangular Transposition
// 9/8/2025
// Example for CIS 3362
// Note: A good exercise is to take the transposed output and recover the plaintext.
//       Write a function decrypt to do this and test it!

import java.util.*;

public class tritranspose {
	
	public static void main(String[] args) {
		
		// Read in string.
		Scanner stdin = new Scanner(System.in);
		System.out.println("Enter string to transpose.");
		char[] plain = stdin.next().toCharArray();
		
		// Transpose and print out.
		char[] reorder = encrypt(plain);
		System.out.println(new String(reorder));
	}

	// Returns a triangular transposition on the string plain.
	public static char[] encrypt(char[] plain) {
	
		// Get the number of rows.
		int n = 0;
		while (n*n < plain.length) n++;
	
		// Fill in an empty grid.
		char[][] temp = new char[n][2*n-1];
		for (int i=0; i<n; i++)
			Arrays.fill(temp[i], '_');
			
		int sI = 0, startCol = n-1, length = 1;
		
		// i is the row index into the 2d grid.
		for (int i=0; i<n; i++) {
		
			// This runs the minimum of the letters left and the odd number of letters this
			// row could support.
			for (int j=startCol; j<startCol+length && sI<plain.length; j++)
				temp[i][j] = plain[sI++];
		
		
			// This always moves to the left by 1.
			startCol--;
			
			// We add two here.
			length += 2;
		}
		
		// Copy the answer into here.
		char[] cipher = new char[plain.length];
		
		// Index  into ciphertext.
		sI = 0;
		
		// Loop through columns here.
		for (int j=0; j<2*n-1; j++) {
			
			// Goes top to bottom on column j.
			for (int i=0; i<n; i++) {
				
				// Just skip empty squares in the grid.
				if (temp[i][j] == '_') continue;
				
				// Copy this in...
				cipher[sI++] = temp[i][j];
			}
		}
	
		return cipher;
	}
}
