題目描述:
有一種技巧可以對數據進行加密,它使用一個單詞作為它的密匙。下面是它的工作原理:首先,選擇一個單詞作為密匙,如TRAILBLAZERS。如果單詞中包含有重復的字母,只保留第1個,其余幾個丟棄?,F在,修改過的那個單詞死于字母表的下面,如下所示:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
T R A I L B Z E S C D F G H J K M N O P Q U V W X Y
上面其他用字母表中剩余的字母填充完整。在對信息進行加密時,信息中的每個字母被固定于頂上那行,并用下面那行的對應字母一一取代原文的字母(字母字符的大小寫狀態應該保留)。因此,使用這個密匙,Attack AT DAWN(黎明時攻擊)就會被加密為TPPTad TP ITVH。輸入描述:輸入key和要加密的字符串
輸出描述:返回加密后的字符串
輸入實例:
nihao
ni
輸出實例:
le
算法實現:
#include<iostream>#include<string>#include<vector>using namespace std;/************************************************ * Author: 趙志乾 * Date: 2017-2-17 * Declaration: All Rigths Reserved !!! ***********************************************/ int main(){ string key,instr; cin>>key>>instr; vector<char>code(26,0); vector<char>state(26,0); int count=0; for(int i=0;i<key.length();i++) { int index=key[i]>='a'?'a':'A'; index=key[i]-index; if(state[index]==0) { state[index]=1; code[count]=index; count++; } } for(int i=0;i<26;i++) { if(state[i]==0) { state[i]=1; code[count]=i; count++; } } string ret=instr; for(int i=0;i<instr.length();i++) { char offset=instr[i]>='a'?'a':'A'; ret[i]=code[instr[i]-offset]+offset; } cout<<ret<<endl; return 0;}
新聞熱點
疑難解答
圖片精選