码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3096 Surprising Strings

时间:2017-07-29 20:25:32      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:height   字符串   span   final   diff   mem   lin   取字符串   判断   

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

 
题目大意定义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

标签:height   字符串   span   final   diff   mem   lin   取字符串   判断   

原文地址:http://www.cnblogs.com/FTA-Macro/p/7253100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!