sicily 1036. Crypto Columns
MEETME
BYTHEO
LDOAKT
REENTH
Here, we've padded the message with NTH. Now the message is printed out by columns, but the columns are printed in the order determined by the letters in the keyword. Since A is the letter of the keyword that comes first in the alphabet, column 2 is printed first. The next letter, B, occurs twice. In the case of a tie like this we print the columns leftmost first, so we print column 1, then column 4. This continues, printing the remaining columns in order 5, 3 and finally 6. So, the order the columns of the grid are printed would be 2, 1, 4, 5, 3, 6, in this case. This output is called the ciphertext, which in this example would be EYDEMBLRTHANMEKTETOEEOTH. Your job will be to recover the plaintext when given the keyword and the ciphertext.
There will be multiple input sets. Each set will be 2 input lines. The first input line will hold the keyword, which will be no longer than 10 characters and will consist of all uppercase letters. The second line will be the ciphertext, which will be no longer than 100 characters and will consist of all uppercase letters. The keyword THEEND indicates end of input, in which case there will be no ciphertext to follow.
0 1 2 3 4 5 /* 在keyword中的出现顺序 */ 1 0 4 2 3 5 /* 在字母表中出现的顺序,重复的先标左边 */ B A T B O Y 将这个顺序(1 0 4 2 3 5)放在order数列里 klen = keyword的长度 将原字符串拆成klen行 0 EYDE 1 MBLR 2 THAN 3 MEKT 4 ETOE 5 EOTH 按照order里的顺序,将对应行塞进ans中 1 MBLR 0 EYDE 4 ETOE 2 THAN 3 MEKT 5 EOTH 竖着打印出来就是答案
AC代码
1 #include<stdio.h> 2 #include<string.h> 3 char ans[12][150] = {0}; 4 const char end[] = "THEEND"; 5 int main() 6 { 7 int i, j, klen, tlen, clen, l; 8 char key[12] = {0}; 9 char text[110] = {0}; 10 int order[10]; 11 while( gets(key) && strcmp(key, end) ) 12 { 13 gets(text); 14 15 memset( ans, 0, sizeof(ans) ); 16 klen = strlen(key); 17 tlen = strlen(text); 18 clen = tlen/klen; 19 20 l = 0; 21 for ( i = 0; i < 26; i++ ) 22 for ( j = 0; j < klen; j++ ) 23 if( key[j] == 'A' + i ) 24 order[j] = l++; 25 26 27 for ( i = 0; i < klen; i++) 28 strncpy( ans[i], text+order[i]*clen, clen ); 29 30 for ( i = 0; i < clen; i++) 31 for ( j = 0; j < klen; j++) 32 printf("%c", ans[j][i] ); 33 34 printf("\n"); 35 } 36 37 return 0; 38 }