Surprising Strings

来源:互联网 发布:单片机资源 编辑:程序博客网 时间:2024/06/05 00:51

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

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.

 

 

 

题意:这题的题意就是给一个没有空格的字符串,在这个字符串中找一对一对的字母,开始是相连的两个字母,那AABB来说,就有三组AA,AB,和BB,然后是中间跳一个字母,就有AB,AB两组数据,最后就只剩下一组数据AB(第一个A和最后一个B)。在这三组测试数据中当中间隔一个字母的那组数据出现了相同的,所以就不是surprising。题目中给的ZGBG,不论是相邻的还是中间空一格,每组数据中都不会出现相同的,所以是suiprising。

思路:因为每次距离加一,所以一共循环n-1次,在这里我学会了一个string类型的作用,string a,a+=str[i],可将字符加到字符串后面成为新的字符串,然后加到map里,在底下在判断是否之前出现过,如果出现则break,其实我的这个代码还不是最方便的,因为只要有一个重复的,整个就NOT surprising,但是我这里用的计数。

代码实现:

#include <stdio.h>#include <string.h>#include <map>#include<queue>#include<string>#include <iostream>#include <algorithm>using namespace std;map<string,int>::iterator it;char str[101];int main(){    int n,m,i,j;    while(~scanf("%s",str)&&strcmp(str,"*")!=0)    {        n=strlen(str);        if(n==1||n==2)        {            printf("%s is surprising.\n",str);        }        else        {            int flag=0;            int k=n-1;            int d=0;            while(k--)            {                map<string,int>q;                for(i=0;i<n-1&&i+d+1<n;i++)                {                    string e;                    e+=str[i];                    e+=str[i+d+1];                    q[e]++;                }                for(it=q.begin();it!=q.end();it++)                {                    if(it->second!=1)                    {                        flag=1;                        break;                    }                }                if(flag==1)                    break;                else                    d++;            }            if(flag)            {                printf("%s is NOT surprising.\n",str);            }            else            {                printf("%s is surprising.\n",str);            }        }    }    return 0;}


 

0 0
原创粉丝点击