POJ 1051

来源:互联网 发布:oracle修改数据库时间 编辑:程序博客网 时间:2024/05/01 09:10
P,MTHBGWB
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6911 Accepted: 3979

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

5AKADTOF_IBOETATUK_IJNPUELQEWOISE.EIVCAEFNRXTBELYTGD.?EJHUT.TSMYGW?EJHOTDSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E

Sample Output

1: ACM_GREATER_NY_REGION2: PERL3: QUOTH_THE_RAVEN,_NEVERMORE.4: TO_BE_OR_NOT_TO_BE?5: THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG

Source

Greater New York 2001

小模拟。。。
打表过大笑
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <ctype.h>#include <vector>#include <queue>#include <set>using namespace std;#define MAXN 10#define Inf 0x7ffffff#define eps 1e10-8#define mod 1000000007typedef long long LL;const double PI = acos(-1.0);typedef double D;//#define Online_Judge#define outstars cout << "***********************" << endl;#define REP(i , n) for(int i = 0 ; i < n ; i++)int n ;char s[120];map<string , char>retran;map<char ,string>tran;int main(){#ifdef Online_Judge    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);#endif // Online_Judge    tran['A'] = ".-";    tran['B'] = "-...";    tran['C'] = "-.-.";    tran['D'] = "-..";    tran['E'] = ".";    tran['F'] = "..-.";    tran['G'] = "--.";    tran['H'] = "....";    tran['I'] = "..";    tran['J'] = ".---";    tran['K'] = "-.-";    tran['L'] = ".-..";    tran['M'] = "--";    tran['N'] = "-.";    tran['O'] = "---";    tran['P'] = ".--.";    tran['Q'] = "--.-";    tran['R'] = ".-.";    tran['S'] = "...";    tran['T'] = "-";    tran['U'] = "..-";    tran['V'] = "...-";    tran['W'] = ".--";    tran['X'] = "-..-";    tran['Y'] = "-.--";    tran['Z'] = "--..";    tran['_'] = "..--";    tran['.'] = "---.";    tran[','] = ".-.-";    tran['?'] = "----";    retran[".-"] = 'A';    retran["-..."] = 'B';    retran["-.-."] = 'C';    retran["-.."] = 'D';    retran["."] = 'E';    retran["..-."] = 'F';    retran["--."] = 'G';    retran["...."] = 'H';    retran[".."] = 'I';    retran[".---"] = 'J';    retran["-.-"] = 'K';    retran[".-.."] = 'L';    retran["--"] = 'M';    retran["-."] = 'N';    retran["---"] = 'O';    retran[".--."] = 'P';    retran["--.-"] = 'Q';    retran[".-."] = 'R';    retran["..."] = 'S';    retran["-"] = 'T';    retran["..-"] = 'U';    retran["...-"] = 'V';    retran[".--"] = 'W';    retran["-..-"] = 'X';    retran["-.--"] = 'Y';    retran["--.."] = 'Z';    retran["..--"] = '_';    retran["---."] = '.';    retran[".-.-"] = ',';    retran["----"] = '?';    scanf("%d",&n);    stack <int> S;    for(int k = 1  ; k <= n ; k++)    {        string s1 , s2 ;        s1 =  "";        while(!S.empty())        {            S.pop();        }        scanf("%s",s);        cout<<k<<": ";        int len = strlen(s);        for(int  i = 0 ; i < len ; i++)        {                s1 += tran[s[i]];                S.push(tran[s[i]].length());        }        int j = 0;        while(!S.empty())        {            int x = S.top();            S.pop();            s2 = "";            while(x--)            {                s2 += s1[j++];            }            cout << retran[s2];        }        cout << endl;    }    return 0;}/**/


原创粉丝点击