首页 > 代码库 > POJ 3096 Surprising Strings
POJ 3096 Surprising Strings
Surprising Strings
64-bit integer IO format: %lld Java class name: Main
The <dfn>D-pairs of a string of letters are the ordered pairs of letters that are distance D from each other. A string is <dfn>D-unique if all of its D-pairs are different. A string is <dfn>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
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.
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <map>13 #include <stack>14 #define LL long long15 #define pii pair<int,int>16 #define INF 0x3f3f3f3f17 using namespace std;18 map<string,int>mp;19 char str[100],tmp[5];20 int main() {21 while(~scanf("%s",str)){22 if(str[0] == ‘*‘) break;23 int len = strlen(str);24 bool flag = true;25 for(int k = 1; k < len; k++){26 mp.clear();27 for(int j = 0; j+k < len; j++){28 tmp[0] = str[j];29 tmp[1] = str[j+k];30 tmp[2] = ‘\0‘;31 if(mp[tmp]) {flag = false;break;}32 mp[tmp]++;33 }34 if(!flag) break;35 }36 if(flag) printf("%s is surprising.\n",str);37 else printf("%s is NOT surprising.\n",str);38 }39 return 0;40 }
POJ 3096 Surprising Strings