POJ 1051 P,MTHBGWB 简单字符串转换

P,MTHBGWB
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5309 Accepted: 3048

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
这道题一直纠结于很多细节的地方,基础实在太差,很久才AC
 
#include<iostream>
#include<string>
using namespace std;
char* map[]={".-","-...","-.-.","-..",".","..-.","--." ,"....","..",".---","-.-",".-..","--","-." ,"---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int main()
{
 char line[101],eline[101],str[5],line1[600];
 int length[101],length1;
 int T;
 cin>>T;
 getchar();
 //在gets之前,做过scanf之类的,scanf处理了数据,却把换行符留在那里没有处理所以你gets得到的就只是一个空行了。
 //getchar()的作用是读一个字符,用了它之后,就刚好吃掉了那个换行符,所以gets得以顺利运行。
 for(int i=0;i<T;i++)
 { 
        int inc=0;
  gets(line);
  if(line[inc]<'A'||line[inc]>'Z')
  {
    switch(line[inc])
    {
    case '_':
               strcpy_s(line1,"..--");
               length[inc]=strlen(line1);
                        length1=strlen(line1);
      break;
    case '.':
               strcpy_s(line1,"---.");
               length[inc]=strlen(line1);
                        length1=strlen(line1);
      break;
    case ',':
               strcpy_s(line1,".-.-");
               length[inc]=strlen(line1);
                        length1=strlen(line1);
      break;
    case '?':
               strcpy_s(line1,"----");
               length[inc]=strlen(line1);
                        length1=strlen(line1);
      break;
            }
  }
  else
  {
     strcpy_s(line1,map[line[inc]-'A']);
  length[inc]=strlen(line1);
  length1=strlen(line1);
  }
  for(int j=inc+1;j<strlen(line);j++)
  {
            if(line[j]>='A'&&line[j]<='Z')
   {
   strcat_s(line1,map[line[j]-'A']);
   length[j]=strlen(line1)-length1;
            length1=strlen(line1);
   }
   else
   {
    switch(line[j])
    {
    case '_':
               strcat_s(line1,"..--");
               length[j]=strlen(line1)-length1;
                        length1=strlen(line1);
      break;
    case '.':
               strcat_s(line1,"---.");
               length[j]=strlen(line1)-length1;
                        length1=strlen(line1);
      break;
    case ',':
               strcat_s(line1,".-.-");
               length[j]=strlen(line1)-length1;
                        length1=strlen(line1);
      break;
    case '?':
               strcat_s(line1,"----");
               length[j]=strlen(line1)-length1;
                        length1=strlen(line1);
      break;
    }
   }
  }
  for(int l=0,r=strlen(line)-1;l<=r;l++,r--)
  {
   int temp=length[l];
   length[l]=length[r];
   length[r]=temp;
  }
  for(int j=1;j<strlen(line);j++)
   length[j]+=length[j-1];
  int k=0,m=0;
        for(int j=0,num=0;j<strlen(line1);j++,k++)
  {
   if(j<length[num])
   str[k]=line1[j];
   else
   {
    str[k]='/0';
                if(strcmp(str,"..--")==0)
     eline[m++]='_';
    else if(strcmp(str,"---.")==0)
     eline[m++]='.';
    else if(strcmp(str,".-.-")==0)
     eline[m++]=',';
    else if(strcmp(str,"----")==0)
     eline[m++]='?';
    
    else 
    {
    
    for(int l=0;l<26;l++)
     if(strcmp(str,map[l])==0)//不要写成str==map[l]
     {
         
      eline[m++]=char(l+'A');
      break;
     }
    }
    k=0;
    num++;
    str[k]=line1[j];
   }
  }
        str[k]='/0';
        if(strcmp(str,"..--")==0)
   eline[m++]='_';
  else if(strcmp(str,"---.")==0)
   eline[m++]='.';
  else if(strcmp(str,".-.-")==0)
   eline[m++]=',';
  else if(strcmp(str,"----")==0)
   eline[m++]='?';
  else 
  {
  for(int l=0;l<26;l++)
  {
   if(strcmp(str,map[l])==0)//str==map[l]
    {
     
      eline[m++]=char(l+'A');
      break;
       }
  }
  }
  eline[m]='/0';
  
  //字符数组一定要记得添加结束符'/0',否则会输出乱码
  cout<<i+1<<": "<<eline<<endl;
  }
 return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值