P,MTHBGWB 水题

来源:互联网 发布:树莓派读取温湿度数据 编辑:程序博客网 时间:2024/06/09 06:47

    今天遇到一道特别恶心的水题,可能是我水平太low,居然用了好久的时间!不过主要是用在看懂题意上了,英文题目好长,好恶心啊!!题目如下:

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

题目翻译:

给你一个摩斯码对照表,26个字母以及下划线‘_’、逗号','、顿号'.'(英文的一个点)、问号'?'分别对应上面和下面表格的摩斯码,输入只会包含大写字母与后面四个符号,输入给你一个测试数据数n,然后是n组数据,每组一行由英文字母和下划线、逗号

、问号、顿号组成的字符串,要你翻译出加密后的内容,其中,翻译过程如下:

第一步:加密,也就是将字母翻译成摩斯密码,然后每个字母记录其对应的摩斯码的长度;

第二步:反转数字串:

第三步:解码,将摩斯码与数字串结合起来对应摩斯码的表格翻译出转换后的字符串即可。

例如:输入:

1

AKADTOF_IBOETATUK_IJN

第一步:加密-->.--.-.--..----..-...--..-...---.-.--..--.-..--...----.232313442431121334242

第二步:反转数字串-->242433121134244313232

第三步:解码翻译成转换后的字符串--> ACM_GREATER_NY_REGION

Ok啦!!

下面是我写了半天的代码,以及别人写的代码,感觉其实差不太多,一并附上:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
#include <cstdio>// P,MTHBGWB#include <iostream>#include <cmath>#include <cstring>#include <algorithm>#include <string>#include <vector>#define INF 0x7fffffff#define FIN 0x80000000using namespace std;const char letter[32] = {'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','_',',','.','?'                        };const char *code[]= {".-","-...","-.-.","-..",".","..-.","--.",                     "....","..",".---","-.-",".-..","--","-.",                     "---",".--.","--.-",".-.","...","-","..-",                     "...-",".--","-..-","-.--","--..",                     "..--",".-.-","---.","----"                    };const int num[] = {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 transforms(char *str){    int len = strlen(str);    int number[105];    int t = 0;    string temp ;    for(int i=0 ; i<len ; i++)//加密    {        for(int j=0 ; j<30 ; j++)        {            if(str[i]==letter[j])            {                temp.append(code[j]);//append方法用于往字符串后添加字符串                number[t++] = num[j];            }        }    }    int k = 0;    for(int i=0 ; i<len ; i++) //解密    {        for(int j=0 ; j<30 ; j++)        {            if((temp.compare(k,number[len-1-i],code[j]))==0)            {                cout<<letter[j];                break;            }        }        k += number[len-1-i] ;    }    cout<<endl;}int main(){    int n;    cin>>n;    char str[105];    for (int i=1; i<=n; i++)//循环输出    {        cin>>str;        cout<<i<<": ";        transforms(str);    }    return 0;}

别人的:

地址:http://blog.csdn.net/ziren235/article/details/1363408

#include<iostream>#include <string>using namespace std;const char *code[]={".-","-...","-.-.","-..",".","..-.","--.",                "....","..",".---","-.-",".-..","--","-.",                "---",".--.","--.-",".-.","...","-","..-",                "...-",".--","-..-","-.--","--..",                "..--",".-.-","---.","----"};void trans(char *source);int main(){    int n;    cin>>n;    char st[100];    for (int i=1;i<=n;i++) {        cin>>st;        cout<<i<<": ";        trans(st);    }    return 0;}void trans(char *source){    int i,j;    int slen=strlen(source);//how many characters in one input line    string temp;//the encryption code series    string dest;    int nseries[100]={0};//the encryption numeric series    for (i=0;i<slen;i++) {        j=source[i]-'A';        if (j==30)     j=26;        if (j==-21) j=27;        if (j==-19) j=28;        if (j==-2)     j=29;        temp.append(code[j]);        nseries[i]=strlen(code[j]);    }    int t=0;    for (j=0;j<slen;j++) {                for (i=0;i<30;i++) {            if ((temp.compare(t,nseries[slen-1-j],code[i]))==0) {                if (i==26)  i=30;                if (i==27)  i=-21;                if (i==28)  i=-19;                if (i==29)  i=-2;                cout<<(char(65+i));                break;            };        }        t+=nseries[slen-1-j];    }    cout<<endl;}

我发现此题有两个以前我不太熟的库函数:compare()和append()
以前刷题的时候见过,不过自己用的还不多,现在用起来发现是蛮好用的!用法如下:str1.compare(str2),字符串str1与str2比较
如果str1.compare(str2)大于零,str1>str2,等于零,str1=str2.否则,str1<str2,还有几种带有数字参数的,为从某个位置开始的
多少个进行子字符串的比较,具体示例如下:
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std; 
  4. int main(void){  
  5.     string str1="hello,world,hello";  
  6.     string str2="hello,world";  
  7.     //字符串比较  
  8.     if(str1.compare(str2)>0)  
  9.         printf("str1>str2\n");  
  10.     else if(str1.compare(str2)<0)  
  11.         printf("str1<str2\n");  
  12.     else  
  13.         printf("str1==str2\n");  
  14.       
  15.     //str1的子串(从索引3开始,包含4个字符)与str2进行比较  
  16.     if(str1.compare(3,4,str2)==0)  
  17.         printf("str1的指定子串等于str2\n");  
  18.     else  
  19.         printf("str1的指定子串不等于str2\n");  
  20.       
  21.     //str1的指定子串与str2的指定子串进行比较,从3开始的4个字符 
  22.     if(str1.compare(3,4,str2,3,4)==0)  
  23.         printf("str1的指定子串等于str2的指定子串\n");  
  24.     else  
  25.         printf("str1的指定子串不等于str2的指定子串\n");  
  26.       
  27.     //str1指定子串(从0开始的2个字符)与指定字符串的前n(2)个字符进行比较  
  28.     if(str1.compare(0,2,"hi,hello",2)==0)  
  29.         printf("str1的指定子串等于指定字符串的前2个字符组成的子串\n");  
  30.     else  
  31.         printf("str1的指定子串不等于指定字符串的前2个字符组成的子串\n");  
  32.     return 0;  
  33.       

另一个函数:append()网字符串尾部添加字符串,用法如下:
运行结果:

  1. #include<iostream>  
  2.   
  3. using namespace std;   
  4.   
  5. int main()  
  6.   
  7. {  
  8.   
  9.     string str1="I like C++";  
  10.   
  11.     string str2=",I like the world.";  
  12.   
  13.     string str3="Hello";  

  14.     string str4="Hello";  

  15.    
  16.      //往字符串str1后添加str2
  17.     str1.append(str2);  
  18.    //往字符串str3后从str2的第11个开始添加7个字符
  19.     str3.append(str2, 11, 7);  
  20.    //往str4后面添加5个‘.’
  21.     str4.append(5, '.');  
  22.     
  23.   
  24.     cout<<str1<<endl;  
  25.   
  26.     cout<<str3<<endl;  
  27.   
  28.     cout<<str4<<endl;  
  29.   
  30.     return 0;     
  31.   
  32. }  

I like C++,I like the world.

Hello World.

Hello.....





 


FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
All Copyright Reserved ©2010-2014 HUST ACM/ICPC TEAM 
Anything about the OJ, please ask in the forum, or contact author:Isun
Server Time: 2017-05-01 09:20:05


  1.     string str3="Hello";  
0 0
原创粉丝点击