现在的位置: 首页 > 综合 > 正文

POJ 1051 ZOJ 1068 字符串处理 P,MTHBGWB

2013年01月21日 ⁄ 综合 ⁄ 共 5070字 ⁄ 字号 评论关闭

题目

题目链接

POJ: http://poj.org/problem?id=1051

ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=68

P,MTHBGWB

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7022   Accepted: 4048

Description

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". 

Input

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.

Output

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.

Sample Input

5
AKADTOF_IBOETATUK_IJN
PUEL
QEWOISE.EIVCAEFNRXTBELYTGD.
?EJHUT.TSMYGW?EJHOT
DSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E

Sample Output

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

Source

题解

题目的意思有点晦涩,不过其实很好理解。通过我们的观察可以发现摩尔斯电码有一种缺陷,就是因为字母由不同长度的点横来代替,所以,有可能出现很多各式各样的解码错误,比如说本来我想说“SOS”,也就是“...---...”,但是很有可能误解成“EEETTTEEE”,显然,不知所云了。因此有人发明了一种信息的加密方式,分为3个步骤:

1,把目标文本转换为没有停顿的摩尔斯电码,并且额外增加一个数字串A记录每一个编码的长度;

2,把这个数字串A前后颠倒得到A';

3,用颠倒后的数字串A'作为每个编码的长度,再把点横转化为文本;

如此一来,相当于规范了一些,至少能够方便传递文本了,或许这是哪个设计者所想到的,反正我不怎么觉得这种编码有什么好处。

好了言归正传,首先你得程序中要有两个部分:电码转明码,码转码。随后,在主要的解题函数中(我的代码里就是solve()了)把这套密文明文转换写进去,应该不会有什么大问题,主要还是细心方面的,算是一个比较简单的处理了。具体的请看代码吧。

代码示例

/****
	*@Polo-shen
	*
	*/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
//#include <algorithm>
#include <cmath>
//#include <iomanip>
//#include <fstream>
//#include <cstdlib>
#include <vector>
//#include <list>
//#include <set>
//#include <map>


using namespace std;
typedef long long int64;


#define DBG 0
#define ALG 0


#if DBG
	#include <cstdlib>
	#define pause system("pause")
	#define ShowLine cout<<__LINE__<<">|\t"
	#define dout cout<<__LINE__<<">|\t"
	#define write(x) #x" = "<<(x)<<" "
	#define awrite(array,num) #array"["<<num<<"]="<<array[num]<<" "
#else
	#define pause ;
	#define ShowLine DBG && cout<<__LINE__<<">|\t"
	#define dout DBG && cout<<__LINE__<<">|\t"
	#define write(x) #x" = "<<(x)<<" "
	#define awrite(array,num) #array"["<<num<<"]="<<array[num]<<" "
#endif // DBG


#if ALG
	#include <algorithm>
#else
	#ifndef min
		#define min(x,y) ((x) < (y) ? (x) : (y))
	#endif
	#ifndef max
		#define max(x,y) ((x) > (y) ? (x) : (y))
	#endif
#endif // ALG


void sprint(string s){
	if (DBG){
		string::size_type sz=s.size();
		for (int i=0;i<sz;i++){
			printf("%c",s[i]);
		}
		puts("|< EOL >|\n");
	}
	else return;
}


enum {
	A = '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
};


enum {
	underscore = '_',
	period = '.',
	comma = ',',
	questionmark = '?'
};


char morseCode[26][6]={
	".-","-...","-.-.","-..",".","..-.","--.",
	"....","..",".---","-.-",".-..","--","-.",
	"---",".--.","--.-",".-.","...","-",
	"..-","...-",".--","-..-","-.--","--.."
};


char isMorseCode(string s){
	int i;
	char res;
	for (i = A; i <= Z; i++){
		if (morseCode[i - 'A'] == s){
			res = (char)i;
			break;
		}
	}
	if (s == "..--") res = '_';
	else if (s == "---.") res = '.';
	else if (s == ".-.-") res = ',';
	else if (s == "----") res = '?';
	return res;
}


char* isString(int c){
	switch (c){
		case underscore:
			return "..--";
		case period:
			return "---.";
		case comma:
			return ".-.-";
		case questionmark:
			return "----";
		default:
			return morseCode[c - 'A'];
	}
}


void solve(int t, string msg){
	printf("%d: ", t);
	int each_code[100];
	string s;
	string tmp="";
	int i, sz=msg.size();
	for (int i = 0; i < sz; i++){
		s = isString(msg[i]);
		each_code[i] = s.size();
		tmp += s;
	}
	for (i = sz - 1; i >= 0; i--){
		printf("%c", isMorseCode(tmp.substr(0, each_code[i])));
		tmp = tmp.substr(each_code[i]);
	}
	printf("\n");
}


int main(){
	int T,tt=0;
	string s;
	scanf("%d", &T);
	while (T--){
		cin>>s;
		solve(++tt, s);
	}
    return 0;
}

抱歉!评论已关闭.