In the last IIUPC there was a problem called Da Vinci Code prepared on the story of the bestselling book of Dan Brown, The Da Vinci Code. Here is another problem based on his another techno-thriller novel Digital Fortress. In this problem, you will be given a cipher text. Your task is to decipher the text using the decrypting technique described below. Let’s take an example. A cipher text is given as follows:
WECGEWHYAAIORTNU
The output will be:
WEAREWATCHINGYOU
For this problem, there are 16 characters in the given cipher text “WECGEWHYAAIORTNU” which is square of 4. These letters have to be arranged in n×n (in this example 4×4) grid and each letter from the given input will be placed in a grid in row major order (1st row, 2nd row, 3rd row, …). When the given cipher text is placed in the grid it looks like as follow:
W E C G
E W H Y
A A I O
R T N U
From the above grid if we take the letters in column major order (1st column, 2nd column, 3rd column, …) then we get the following decrypted text:
WEAREWATCHINGYOU
Input
Input starts with a line consisting of a single number T. T test cases follow. Each test case consists of one line. This line contains the cipher text. The cipher text contains either UPPERCASE letters or blank spaces. Total number of character in the text will not be more 10,000.
Output
For each test case, the output contains a single line containing the decrypted text. If the number of characters in the input text is not square of any number, then give the output ‘INVALID’.
Sample Input
3
WECGEWHYAAIORTNU
DAVINCICODE
DTFRIAOEGLRSI TS
Sample Output
WEAREWATCHINGYOU
INVALID
DIGITAL FORTRESS
问题链接:UVA11716 Digital Fortress
问题简述:(略)
问题分析:
。
。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA11716 Digital Fortress */
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
char s[N + 1];
int main()
{
int t;
scanf("%d", &t);
getchar();
for(int k = 1; k <= t; k++) {
gets(s);
int len = strlen(s);
int n = sqrt(len + 0.1);
if(n * n != len)
printf("INVALID\n");
else {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
putchar(s[j * n + i]);
putchar('\n');
}
}
return 0;
}