当前位置:编程学习 > C/C++ >>

POJ 3096 Surprising Strings

Surprising Strings
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4813 Accepted: 3175
Description
The D-pairs of a string of letters are the ordered pairs of letters that are distance D from each other. A string is D-unique if all of its D-pairs are different. A string is surprising if it is D-unique for every possible distance D.
Consider the string ZGBG. Its 0-pairs are ZG, GB, and BG. Since these three pairs are all different, ZGBG is 0-unique. Similarly, the 1-pairs of ZGBG are ZB and GG, and since these two pairs are different, ZGBG is 1-unique. Finally, the only 2-pair of ZGBG is ZG, so ZGBG is 2-unique. Thus ZGBG is surprising. (Note that the fact that ZG is both a 0-pair and a 2-pair of ZGBG is irrelevant, because 0 and 2 are different distances.)
Acknowledgement: This problem is inspired by the "Puzzling Adventures" column in the December 2003 issue of Scientific American.
Input
The input consists of one or more nonempty strings of at most 79 uppercase letters, each string on a line by itself, followed by a line containing only an asterisk that signals the end of the input.
Output
For each string of letters, output whether or not it is surprising using the exact output format shown below.
Sample Input
ZGBG
X
EE
AAB
AABA
AABB
BCBABCC
*
Sample Output
ZGBG is surprising.
X is surprising.
EE is surprising.
AAB is surprising.
AABA is surprising.
AABB is NOT surprising.
BCBABCC is NOT surprising.
Source
Mid-Central USA 2006
      刚做这题的时候我本来是想用字典树的来写的,但是在写的时候我突然意识到那个字串就两个字符 完全可以看成是一个27进制的数,这个数有两位数。标记一下不就行了嘛
[cpp]  
#include <stdio.h>  
#include <string.h>  
#include <math.h>  
char s1[100],a[1000];  
int main()  
{  
    int i,j,n,m,s,t,l;  
    char c1,c2;  
    while(scanf("%s",s1)!=EOF)  
    {  
        if(strcmp(s1,"*")==0)  
        {  
            break;  
        }  
        l=strlen(s1);  
        for(i=1;i<=l-1;i++)  
        {  
            memset(a,0,sizeof(a));  
            for(j=0;j<=l-1;j++)  
            {  
               if(j+i>l-1)  
               {  
                   break;  
               }  
               c1=s1[j];  
               c2=s1[j+i];  
               s=(int)((c1-'A'))*27+(int)((c2-'A'));  
               if(!a[s])  
               {  
                   a[s]=1;  
               }else  
               {  
                   break;  
               }  
            }  
            if(j+i<=l-1)  
            {  
                break;  
            }  
        }  
        if(i==l)  
        {  
            printf("%s is surprising.\n",s1);  
        }else  
        {  
            printf("%s is NOT surprising.\n",s1);  
        }  
    }  
    return 0;  
}  
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,