[sicily online]1036. Crypto Columns

來源:互聯網
上載者:User
/*ConstraintsTime Limit: 1 secs, Memory Limit: 32 MB DescriptionThe 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 write the message (ignoring spacing and punctuation) in a grid with 6 columns, padding with random extra letters as needed: MEETMEBYTHEOLDOAKTREENTHHere, 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.InputThere 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.OutputFor 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 InputBATBOYEYDEMBLRTHANMEKTETOEEOTHHUMDINGEIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEXTHEENDSample OutputMEETMEBYTHEOLDOAKTREENTHONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX*/#include<iostream>#include<cmath>#include<iomanip>#include<string>#include<vector>#include<algorithm>using namespace std;int main(){string keyword,ciphertext;while(cin>>keyword&&keyword!="THEEND"){vector< pair<char,int> > key;for(string::size_type i=0;i<keyword.size();i++){key.push_back(make_pair(keyword[i],i));}sort(key.begin(),key.end());cin>>ciphertext;string text(ciphertext.size(),'0');for(string::size_type i=0;i<key.size();i++){for(string::size_type j=0;j<ciphertext.size()/keyword.size();j++)text[key[i].second*ciphertext.size()/keyword.size()+j]=ciphertext[i*ciphertext.size()/keyword.size()+j];}for(string::size_type i=0;i<ciphertext.size()/keyword.size();i++){for(string::size_type j=0;j<key.size();j++)cout<<text[j*ciphertext.size()/keyword.size()+i];}cout<<endl;}//end while}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.