1036. crypto Columns 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 your strated 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 write the message (ignoring spacing and punctuation) in a grid with 6 columns, padding with random extra letters as needed:
Meetme
Bytheo
Ldoach
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 wocould be 2, 1, 4, 5, 3, 6, in this case. this output is called the ciphertext, which in this example wocould be eydemo-rthanmektetoeeoth. 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 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.
Output
For each input set, output one line that contains the plaintext (with any characters that were added for padding). This line shoshould contain no spacing and shoshould be all uppercase letters.
Sample Input
BATBOYEYDEMBLRTHANMEKTETOEEOTHHUMDINGEIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEXTHEEND
Sample output
MEETMEBYTHEOLDOAKTREENTHONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX
# Include <iostream> # include <algorithm> # include <map> # include <cstring> using namespace STD; bool CMP (const char a, const char B) {return a <B ;}int main () {string keyword; string ciphertext; while (CIN >>> keyword & keyword. compare ("theend ")! = 0) {CIN> ciphertext; Map <int, char> key_order; For (INT I = 0; I <keyword. length (); I ++) {key_order [I] = keyword [I]; // I indicates that the substring corresponding to this character in keyword should be in the column} Sort (keyword. begin (), keyword. end (), CMP); // sort the keyword in ascending order, so that each character corresponds to the string sub_strs [keyword. length ()]; // character column int sub_s = 0; // start coordinate of each string int sub_e = ciphertext. length ()/keyword. length (); // The ending coordinate bool visited [keyword. length ()]; // visited is used to record the characters that have been accessed to avoid memset (visited, false, sizeof (visited); For (INT I = 0; I <keyword. length (); I ++) {// traverse the sorted keyword // scan the key_order for each character to determine the column in which the character in the keyword is placed for (Int J = 0; j <keyword. length (); j ++) {// column in which the corresponding substring is put according to key_order, that is, if (key_order [J] = keyword [I] & visited [J] = false) {sub_strs [J] = ciphertext. substr (sub_s, sub_e); sub_s = sub_e; sub_e + = ciphertext. length ()/keyword. length (); visited [J] = true; break ;}}// from left to right, output for (INT I = 0; I <ciphertext. length ()/keyword. length (); I ++) {for (Int J = 0; j <keyword. length (); j ++) cout <sub_strs [J]. at (I) ;}cout <Endl;} return 0 ;}
Sicily 1036 string decoding array and subscript