ACM (UVa) : 11716


// http://uva.onlinejudge.org/external/117/11716.html

#include <stdio.h>
#include <string.h>
#include <math.h>

int main ()
{
    int testCase;
    scanf ("%d", &testCase);
    getchar ();

    while ( testCase-- ) {
        char a [10010];

        gets (a);

        int length = strlen (a);
        double square_root = sqrt (length);
        int temp_root = square_root;

        if ( square_root - temp_root != 0 )
            printf ("INVALID");

        else {
            char grid [102] [102];

            int index = 0;

            for ( int i = 0; i < temp_root; i++ ) {
                for ( int j = 0; j < temp_root; j++ )
                    grid [i] [j] = a [index++];
            }

            for ( int i = 0; i < temp_root; i++ ) {
                for ( int j = 0; j < temp_root; j++ )
                    printf ("%c", grid [j] [i]);
            }

        }

        printf ("\n");
    }

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s