1051 P,MTHBGWB

用两个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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值