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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章