Click Here to go back to the homepage.

Secret Message Solution:


#include <bits/stdc++.h>
using namespace std;


int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    int nCases;
    cin >> nCases;
    while(nCases--){
        string line;
        cin >> line;
        double sqrtLen = sqrt(line.size());
        int sideLength = sqrtLen - (int) sqrtLen == 0 ? (int) sqrtLen : (int) sqrtLen + 1;
        char table[sideLength][sideLength] = {};
        int index = 0;

        for(int i = sideLength -1; i >= 0; i--){
            for(int j = 0; j < sideLength; j++){
                if(index == line.size()){
                    goto loopEnd;
                }
                table[j][i] = line.at(index++);
            }
        }
        loopEnd:
        string result;
        for(int i = 0; i < sideLength; i++){
            for(int j = 0; j < sideLength; j++){
                char c = table[i][j];
                if(c){
                    result.push_back(c);
                }
            }
        }
        cout << result << endl;
    }
    
    return 0;   
}