HDOJ HDU 1982 Kaitou Kid - The Phantom Thief(1) ACM 1982 IN HDU

余生颓废 提交于 2020-03-21 09:01:45
//MiYu原创, 转帖请注明 : 转载自 ______________白白の屋

题目地址 :
         http://acm.hdu.edu.cn/showproblem.php?pid=1982

PE了N次, 很纠结的一个题........  题目如下 :

Problem Description
Do you know Kaitou Kid? In the legend, Kaitou Kid is a master of disguise, and can take on the voice and form of anyone. He is not an evil person, but he is on the wrong side of the law. He's the very elusive phantom thief who never miss his prey although he always uses word puzzles to announce his targets before action.

You are the leader of a museum. Recently, you get several priceless jewels and plan to hold an exhibition. But at the moment, you receive Kid's word puzzle... Fortunately, It seems Kid doesn’t want to trouble you, and his puzzle is very easy. Just a few minutes, You have found the way to solve the puzzle:

(1) change 1 to 'A', 2 TO 'B',..,26 TO 'Z'
(2) change '#' to a blank
(3) ignore the '-' symbol, it just used to separate the numbers in the puzzle

Input
The first line of the input contains an integer C which means the number of test cases. Then C lines follow. Each line is a sentence of Kid’s word puzzle which is consisted of '0' ~ '9' , '-' and '#'. The length of each sentence is no longer than 10000.

Output
For each case, output the translated text.

Sample Input
4
9#23-9-12-12#19-20-5-1-12#1-20#12-5-1-19-20#15-14-5#10-5-23-5-12
1-14-4#12-5-1-22-5#20-8-5#13-21-19-5-21-13#9-14#20#13-9-14-21-20-5-19
1-6-20-5-18#20-8-5#15-16-5-14-9-14-7#15-6#20-8-5#5-24-8-9-2-9-20-9-15-14
7-15-15-4#12-21-3-11

Sample Output
I WILL STEAL AT LEAST ONE JEWEL
AND LEAVE THE MUSEUM IN T MINUTES
AFTER THE OPENING OF THE EXHIBITION
GOOD LUCK


刚开始是用的库函数 strtok 对字符串进行处理,  直接敲完,没有出现错误, 提交,悲剧开始了


下面的是PE 的代码 :
//MiYu原创, 转帖请注明 : 转载自 ______________白白の屋

#include 
<iostream>
#include 
<cstdlib>
#include 
<cstring>
using namespace std;
char psw[10005];
char sym[133];
void setSym ( )
{
     
int i;
     
char ch;
     
for ( ch = 'A', i = 1; i <= 26++ i , ++ ch )
     {
           sym[i] 
= ch ;
     } 

string prs ( char *psw )
{
     
string str;
     
int n = strlen ( psw );
     
int num = 0;
     
for ( int i = 0; i != n; ++ i )
     {
           
if ( psw[i] != '-' )
           {
                num 
= num * 10 + psw[i] - '0'
           } 
           
else 
           {
                
if ( num != 0 )
                {
                     str 
+= sym[num];
                }
                num 
= 0
           }
     }
     
if ( num != 0 )
     {
          str 
+= sym[num];
     }
     
return str;
}
int main ()
{
    setSym ();
    
int T;
    
while ( scanf ( "%d\n",&T ) != EOF )
    {
            
while ( T -- )
            {
                    gets ( psw );
                    
string str;
                    
char *ptr = strtok ( psw, "#" );
                    
if ( strlen ( ptr ) != 0 )
                         str 
= prs ( ptr ); 
                    
while ( ptr = strtok ( NULL, "#" ) )
                    {
                           
if ( strcmp ( ptr, "" ) != 0 )
                           {
                                str 
+= " ";
                                str 
+= prs ( ptr ); 
                           }
                    }
                    cout 
<< str << endl;; 
            }           
    }
    
return 0
}

 最后在Ambition 大牛的提示下,成功AC, 因为strtok是忽视被截字符串的个数的 "-----######---##-#-#"
这组数据应该输出10个空格, 而我的代码值能输出3个.
下面的是AC代码 :
//MiYu原创, 转帖请注明 : 转载自 ______________白白の屋

#include 
<iostream>
#include 
<cstdlib>
#include 
<cstring>
using namespace std;
char psw[20005];
char temp[20005];
char sym[133];
void setSym ( )
{
     
int i;
     
char ch;
     
for ( ch = 'A', i = 1; i <= 26++ i , ++ ch )
     {
           sym[i] 
= ch ;
     } 

string prs ( char *psw )
{
     
string str;
     
int n = strlen ( psw );
     
int num = 0;
     
for ( int i = 0; i != n; ++ i )
     {
           
if ( psw[i] != '-' )
           {
                num 
= num * 10 + psw[i] - '0'
           } 
           
else 
           {
                
if ( num != 0 )
                {
                     str 
+= sym[num];
                }
                num 
= 0
           }
     }
     
if ( num != 0 )
     {
          str 
+= sym[num];
     }
     
return str;
}
int main ()
{
    setSym ();
    
int T;
    
while ( scanf ( "%d",&T ) != EOF )
    {
            getchar ();
            
while ( T -- )
            {
                    gets ( psw );
                    
int len = strlen ( psw );
                    
int beg = 0;
                    memset ( temp, 
'\0'sizeof ( temp ) );
                    
string str;
                    
while ( psw[beg] != '\0' )
                    {
                           
int i = 0
                           
while ( psw[beg] != '#' && psw[beg] != '\0' )
                           {
                                   temp[i
++= psw[beg++]; 
                           }
                           temp[i] 
= '\0';
                           
string t = prs ( temp );
                           
if ( t.size() != 0 )
                           {
                                str 
+= t;
                                
if ( psw[beg] == '#' )
                                {
                                     str 
+= " "
                                }  
                           }
                           
else if ( psw[beg] == '#' )
                           {
                                str 
+= " "
                           }
                           beg 
++;
                    }
                    cout 
<< str << endl;
                    memset ( psw, 
'\0'sizeof ( psw ) );
            }           
    }
    
return 0
}

弄了一个下午加一个晚上才 AC , 是自己把简单问题想太复杂了............
Roowe 神牛代码 :
//MiYu原创, 转帖请注明 : 转载自 ______________白白の屋

#include 
<iostream>
#include 
<stdio.h>
#include 
<string.h>
#include 
<ctype.h>
using namespace std;
char str[27]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char s[10001];
int main()
{
    
int T,len,i,num;
    scanf(
"%d",&T);
    getchar();
    
while(T--)
    {
        gets(s);
        len
=strlen(s);
        
for(i=0;i<len;i++)
        {
            
if(isdigit(s[i]) && isdigit(s[i+1]))
            {
                num
=10*(s[i]-'0')+s[i+1]-'0';
                printf(
"%c",str[num-1]);
                i
++;
                
continue;
            }
            
if(isdigit(s[i]) && !isdigit(s[i+1]))
            {
                num
=s[i]-'0';
                printf(
"%c",str[num-1]);
                
continue;
            }
            
if(s[i]=='#')   printf(" ");
        }
        printf(
"\n");
    }
    
return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!