**UVA Problem 11716 – Digital Fortress Solution:**

Click here to go to this problem in uva Online Judge.

**Solving Technique:**

This is an easier string problem. The task is to arrange the characters within the string in a certain order.

**If the input string length is not square of a number then print INVALID. Ex: “DAVINCICODE” has length of 11. Squaring no number results in 11.**

**“DTFRIAOEGLRSI TS” has length of 16 including the space character. Square root of 16 is 4 and 4 * 4 is 16. So this is valid input.
**

Now if the input is valid then next task is to rearrange them. Instead of following given solution technique in the question it can solved much faster in another way. Following instruction in the question gives intuition that first the string must be positioned on 2 dimensional matrix in row major order. Then traverse them column by column.

A better way is find out each group length from square rooting the original string length. Next starting from first group take a character then skip by group length to get next column major character. After its done do this from the next character of first group.

##### Visualization:

** Important: **Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer. Please compile with c++ compiler as some of my codes are in c and some in c++.

More Inputs of This Problem on uDebug.

**Input:**

3 WECGEWHYAAIORTNU DAVINCICODE DTFRIAOEGLRSI TS

**Output:**

WEAREWATCHINGYOU INVALID DIGITAL FORTRESS

### Code:

/** * Author: Asif Ahmed * Site: https://quickgrid.wordpress.com * Problem: UVA 11716 - Digital Fortress * Technique: Square String Traverse from row major * to column major by skipping. */ #include<stdio.h> #include<string.h> #include<math.h> #define N 10002 static char s[N]; static char output[N]; int main(){ //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int n; scanf("%d", &n); getchar(); while( n-- ){ gets(s); // Get the length of string and length of each string group. int len = strlen(s); int rc = sqrt(len); // Reset in case there are characters from previous iteration. memset(output, 0, sizeof output); // If the string length can be divided into equal parts // then traverse by skipping certain length. if( rc * rc == len ){ int k = 0; for( int j = 0; j < rc; ++j ){ for( int i = j; i < len; i = i + rc ){ output[k++] = s[i]; } } puts(output); } else{ puts("INVALID"); } } return 0; }