POJ 1051:P,MTHBGWB(模拟类题目)

总时间限制: 
1000ms
内存限制: 
65536kB
描述
Morse code represents characters as variable length sequences of dots and dashes. In practice, characters in a message are delimited by short pauses. The following table shows the Morse code sequences:
A .- H .... O --- V ...-
B -... I .. P .--. W .--
C -.-. J .--- Q --.- X -..-
D -.. K -.- R .-. Y -.--
E . L .-.. S ... Z --..
F ..-. M -- T -    
G --. N -. U ..-    

Note that four dot-dash combinations are unassigned. For the purposes of this problem we will assign them as follows (these are not the assignments for actual Morse code):
underscore ..-- period ---.
comma .-.- question mark ----

Thus, the message "ACM_GREATER_NY_REGION" is encoded as:
.- -.-. -- ..-- --. .-. . .- - . .-. ..-- -. -.-- ..-- .-. . --. .. --- -.
M.E. Ohaver proposed an encryption scheme based on mutilating Morse code. Her scheme replaces the pauses between letters, necessary because Morse is a variable-length encoding that is not prefix-free, with a string that identifies the number of dots and dashes in each. For example, consider the message ".--.-.--". Without knowing where the pauses should be, this could be "ACM", "ANK", or several other possibilities. If we add length information, however, ".--.-.--242", then the code is unabiguous. 
Ohaver's scheme has three steps, the same for encryption and decryption:
1. Convert the text to Morse code without pauses but with a string of numbers to indicate code lengths
2. Reverse the string of numbers
3. Convert the dots and dashes back into to text using the reversed string of numbers as code lengths
As an example, consider the encrypted message "AKADTOF_IBOETATUK_IJN". Converting to Morse code with a length string yields ".--.-.--..----..-...--..-...---.-.--..--.-..--...----.232313442431121334242". Reversing the numbers and decoding yields the original message "ACM_GREATER_NY_REGION".
输入
This problem requires that you implement Ohaver's encoding algorithm. The input will consist of several messages encoded with Ohaver's algorithm. The first line of the input is an integer n that specifies the number of test cases. The following n lines contain one message per line. Each message will use only the twenty-six capital letters, underscores, commas, periods, and question marks. Messages will not exceed 100 characters in length.
输出
For each message in the input, output the line number starting in column one, a colon, a space, and then the decoded message. The output format must be adhered to precisely.
样例输入
5
AKADTOF_IBOETATUK_IJN
PUEL
QEWOISE.EIVCAEFNRXTBELYTGD.
?EJHUT.TSMYGW?EJHOT
DSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E
样例输出
1: ACM_GREATER_NY_REGION
2: PERL
3: QUOTH_THE_RAVEN,_NEVERMORE.
4: TO_BE_OR_NOT_TO_BE?

5: THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG

为了准备推免机试,开始坚持刷题,先从模拟题练起。

#include <iostream>
#include <string.h>
using namespace std;
char morse[30][6]={".-","-...","-.-.","-..",".","..-.","--.",
                   "....","..",".---","-.-",".-..","--","-.",
				   "---",".--.","--.-",".-.","...","-","..-",
				   "...-",".--","-..-","-.--","--..","..--","---.",
				   ".-.-","----"};
char ch[30]={'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',
			 '_','.',',','?'};
int num[30]={2,4,4,3,1,4,3,4,2,4,3,4,2,2,3,4,4,3,3,1,3,4,3,4,4,4,4,4,4,4};
void temconvert(char *in,char *code,int n[],int len){
	int k=0;
	int p=0;
	for(int i=0;i<len;i++){
		if(in[i]>=65&&in[i]<=90){
			k=in[i]-65;
		}
		else{
			if(in[i]=='_'){
				k=26;
			}
			else if(in[i]=='.'){
				k=27;
			}
			else if(in[i]==','){
				k=28;
			}
			else{
				k=29;
			}
		}
		n[i]=num[k];
		for(int j=0;j<num[k];j++){
			code[p]=morse[k][j];
			p++;
		}
	}
	code[p]='\0';
}
void convert(char *out,char *code,int n[],int len){
	int k;
	int p=0;
	int q=0;
	char temp[6];
	for(int i=0;i<len;i++){
		k=n[len-1-i];
		while(q<k){
			temp[q]=code[p];
			temp[q+1]='\0';
			p++;
			q++;
		}
		q=0;
		for(int j=0;j<30;j++){
			if(strcmp(morse[j],temp)==0){
				out[i]=ch[j];
				out[i+1]='\0';
				break;
			}
		}
	}
}
int main(){
	char input[102];
	char output[102];
	int chnum[102];
	char morsecode[602];
	int N;
	cin>>N;
	int kk=1;
	while(N--){
		cin>>input;
		int len=strlen(input);
		temconvert(input,morsecode,chnum,len);
		convert(output,morsecode,chnum,len);
		cout<<kk<<": "<<output<<endl;
		kk++;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiao_chen_l/article/details/80859175
今日推荐