SOJ 1036. Crypto Columns
2013-08-25 01:03
253 查看
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
The columnar encryption scheme scrambles the letters in a message (or plaintext) using a keyword as illustrated in the following example: Suppose BATBOY is the keyword and our message is MEET ME BY THE OLD OAK TREE. Since the keyword has 6 letters, we writethe message (ignoring spacing and punctuation) in a grid with 6 columns, padding with random extra letters as needed:
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.
Input
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 longerthan 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.
Output
For each input set, output one line that contains the plaintext (with any characters that were added for padding). This line should contain no spacing and should be all uppercase letters.
Sample Input
BATBOY EYDEMBLRTHANMEKTETOEEOTH HUMDING EIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEX THEEND
Sample Output
MEETMEBYTHEOLDOAKTREENTH ONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX
#include<iostream> #include<memory.h> using namespace std; int main() { string keyword; while(cin>>keyword&&keyword!="THEEND") { string matrix; cin>>matrix; int size=matrix.length()/keyword.length(); char matr[101][101]; int keysize=keyword.length(); //cout<<size<<" "<<keysize<<endl; char aa='A'; int index=0; bool flag=false; bool visited[101]; memset(visited,false,sizeof(visited)); while(aa<='Z') { flag=false; for (int i = 0; i < keysize; ++i) { if(keyword[i]==aa&&!visited[i]) { for (int s = 0; s < size; ++s) { /* code */ //cout<<index*size+s<<" is the current index, ele is "<<matrix[index*size+s]<<endl; matr[s][i]=matrix[index*size+s]; } visited[i]=true; // cout<<visited[i]<<endl; flag=true; index++; } } if(!flag) aa++; } for (int i = 0; i < size; ++i) { /* code */ for (int j = 0; j < keysize; ++j) { /* code */ cout<<matr[i][j]; } } cout<<endl; } }//算法的思路是:存储两个字符串,主要耽误时间的是在while循环的判断上,没有仔细考虑,结果导致了死循环,visited记录是否访问过keyword上面该点。
相关文章推荐
- SOJ 3664 chess towers 【完全背包变形】
- soj 2012. King
- soj 3316: Windy's Dates
- TOJ 3486 SOJ 1009 母函数
- SOJ 1014二维最大子矩阵
- soj 1343. Jam的计数法
- soj 1704. Pencils from the Nineteenth Century
- soj 2302. Queue Implementation Using a Circular Array
- Next Mayor--SOJ 2013week2.1000
- soj 2013 weekly-4 暨校赛模拟II 7904-7911
- Sicily 1485 Hanoi双塔问题 (SOJ 1485) 【高精度】
- SOJ 4110: PE class
- SOJ 题目分类
- SOJ--1117: 最大整数
- SOJ 1021 Couples
- SOJ - 11598
- soj 4392 double类型的使用和二分查找
- soj 1050
- SOJ.Subtraction
- soj 2749 The Fewest Coins(多重背包 + 完全背包)