您的位置:首页 > 其它

1051 P,MTHBGWB

2016-06-11 11:16 295 查看
用两个map存储加密和解密信息。

一个stack<int>存储字符长度,加密时长度入栈,解密时长度出栈,符合先进后出。

#pragma warning (disable : 4786)

#include<iostream>
#include<string>
#include<map>
#include<cstdio>
#include<stack>

using namespace std;

map<char,string> morse;
map<string,char> remorse;

stack<int> num;

void init(){

morse['A'] = ".-";
morse['B'] = "-...";
morse['C'] = "-.-.";
morse['D'] = "-..";
morse['E'] = ".";
morse['F'] = "..-.";
morse['G'] = "--.";
morse['H'] = "....";
morse['I'] = "..";
morse['J'] = ".---";
morse['K'] = "-.-";
morse['L'] = ".-..";
morse['M'] = "--";
morse['N'] = "-.";
morse['O'] = "---";
morse['P'] = ".--.";
morse['Q'] = "--.-";
morse['R'] = ".-.";
morse['S'] = "...";
morse['T'] = "-";
morse['U'] = "..-";
morse['V'] = "...-";
morse['W'] = ".--";
morse['X'] = "-..-";
morse['Y'] = "-.--";
morse['Z'] = "--..";
morse['_'] = "..--";
morse['.'] = "---.";
morse[','] = ".-.-";
morse['?'] = "----";
remorse[".-"] = 'A';
remorse["-..."] = 'B';
remorse["-.-."] = 'C';
remorse["-.."] = 'D';
remorse["."] = 'E';
remorse["..-."] = 'F';
remorse["--."] = 'G';
remorse["...."] = 'H';
remorse[".."] = 'I';
remorse[".---"] = 'J';
remorse["-.-"] = 'K';
remorse[".-.."] = 'L';
remorse["--"] = 'M';
remorse["-."] = 'N';
remorse["---"] = 'O';
remorse[".--."] = 'P';
remorse["--.-"] = 'Q';
remorse[".-."] = 'R';
remorse["..."] = 'S';
remorse["-"] = 'T';
remorse["..-"] = 'U';
remorse["...-"] = 'V';
remorse[".--"] = 'W';
remorse["-..-"] = 'X';
remorse["-.--"] = 'Y';
remorse["--.."] = 'Z';
remorse["..--"] = '_';
remorse["---."] = '.';
remorse[".-.-"] = ',';
remorse["----"] = '?';

}

int main(){
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);

init();

int N;
cin>>N;

for(int i=0;i<N;i++){

cout<<i+1<<": ";

string input;
cin>>input;

string trans="";

num.empty();
for(int i=0;i<input.size();i++){
trans += morse[input.at(i)];
num.push(morse[input.at(i)].size());
}

//		cout<<trans<<endl;

int index = 0;
while(!num.empty()){
string output="";
//			cout<<num.top();

int n = num.top();

while(n>0){
output += trans[index];
index++;
n--;
}
//			cout<<output;
cout<<remorse[output];
num.pop();

}
cout<<endl;

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: