Surprising Strings(POJ -3096

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.


题意:给你一个字符串,将该字符串分成距离全为D的字符对,如果这些字符对都不相同则称该字符串为D-unique。如

果对于该字符串所有可能的D距离字符串都是不同的,做称该字符串是令人惊讶的。


思路:暴力可能出现的D距离,然后对于每个D距离的字符对进行map标记,只要出现重复的就说明该字符串不是令人惊

讶的则可直接跳出暴力循环。


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.

<span style="font-size:18px;">#include <cstdio>
#include <map>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    char st[100];
    while(~scanf("%s",st))
    {
        if(st[0]=='*')
            break;
        map<string,bool>flag;                  //定义一个map容器
        bool ff=true;
        int len=strlen(st);
        if(len<=2)
        {
            printf("%s is surprising.\n",st);
            continue;
        }
        for(int i=1; i<len; i++)                   //枚举距离
        {
            flag.clear();                                //清空容器
            for(int j=0; j+i<len; j++)
            {
                char s[5];
                s[0]=st[j];
                s[1]=st[j+i];
                s[2]='\0';
                if(!flag[s])
                    flag[s]=true;
                else
                {
                    ff=false;
                    break;
                }
            }
            if(!ff)
                break;
        }
        if(ff)
            printf("%s is surprising.\n",st);
        else
            printf("%s is NOT surprising.\n",st);

    }
    return 0;
}</span>



发布了187 篇原创文章 · 获赞 15 · 访问量 7万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章