Surprising strings
| Time limit:1000 ms |
|
Memory limit:65536 K |
| Total submissions:6017 |
|
Accepted:3934 |
Description
TheD-pairsOf a string of letters are the ordered pairs of letters that are distance d from each other. A string isD-uniqueIf all of its D-pairs are different. A string isSurprisingIf 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 wrongly red by the "puzzling Adventures" column in the December 2003 issueScientific 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
ZGBGXEEAABAABAAABBBCBABCC*
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.
A: Give You A string, such as ABCD. The strings with a distance of 0 include AB, BC, and CD. The strings with a distance of 1 include AC, BC, and the strings with a distance of 2 include ad, if the two strings are not at the same distance, the string is surprising; otherwise, the string is not surprising;
#include <iostream>#include <string.h>using namespace std;int main(){char s[100];while (cin>>s){if (strcmp(s,"*")==0)break;int len=strlen(s),flag=0;for (int i=0;i<len;i++){for (int j=i+1;j<len;j++){if (s[i]==s[j]){for (int k=1;k<len-j;k++){if (s[k+i]==s[k+j]){flag=1;goto A;}}}}}A:if (flag==0)cout<<s<<" is surprising."<<endl;elsecout<<s<<" is NOT surprising."<<endl;}return 0;}
Poj 3096 surprising strings