首页 > 代码库 > POJ 3096 Surprising Strings

POJ 3096 Surprising Strings

Surprising Strings
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7225   Accepted: 4663

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
 
题目大意定义D-part是表示取字符串s中相距为D的两个字母所构成的字母对,该字母对中两个字母的位置顺序与他们在主串s中的位置顺序一致。对于不同的D,若D-pair中没有相同的字母对,则称为D-unique。若所有的D-pair都是unique的话则该字符串是surprising,否则是not surprising。
 
大致思路:用map寄存字母对是否surprising,具体看代码注释
 
 1 #include<iostream>
 2 #include<map>
 3 #include<cstring>
 4 using namespace std;
 5 map<string,int> m;
 6 char s[80];
 7 int main()
 8 {
 9     while(cin>>s&&s[0]!=*)
10     {
11         int len=strlen(s);
12         int mark=1;//判断是否所有的D-part都unique
13         for(int d=0;d<=len-2;d++)//用一个外循环来表示D(要取的字母之间距离)
14         {
15             map<string,int> m;
16             int flag=1;//判断D-part是否unique
17             for(int i=0;i<=len-d-2;i++)//这个循环用来储存字母对,其中i为字母对第一个字母的下标
18             {
19                 char sp[3]={s[i],s[i+d+1],\0};//这里注意在字母对后要加‘\0‘
20                 if(!m[sp])//m[sp]在一开始输入时都参数为0,利用这个可以把所有不同的字母对对应的参数都变为1
21                     m[sp]=1;
22                 else
23                 {
24                     flag=0;
25                     break;
26                 }
27             }
28             if(!flag)
29             {
30                 mark=0;
31                 break;
32             }
33         }
34         if(mark)
35             cout<<s<<" is surprising."<<endl;
36         else cout<<s<<" is NOT surprising."<<endl;
37     }
38 }

 

POJ 3096 Surprising Strings