Surprising Strings (STL+map)

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

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.

题意描述:在一个字符串中找到间隔为1-字符串长度的一半。的两个字符组成一个字符串,如果这个字符串在相同间隔的情况下没有完全相同的另一个字符串;且在任意间隔下都没有与本间隔相同的字符串则这个字符串就是“is NOT surprising.”,否则就是“is surprising.”的;

解题思路:用双重for循环,外层的用来记录间隔,里面的用来遍历字符串;每次找到的两个字符按顺序放到字符串中然后用map来计数;如果出现过就定义为第一种类型;

扫描二维码关注公众号,回复: 11595621 查看本文章

注意:map的创建位置,因为是判断每个间隔的相同与否,所以map的创建位置和用法很重要;

代码:

#include<stdio.h>
#include<string>
#include<iostream>
#include<string.h>
#include<map>
using namespace std;
char ss[3];
int Ma(char s[])
{
	ss[2]='\0';
	int i,j;
	int len=strlen(s);
	if(len==1 || len==2)
	return 1;
	for(i=1;i<len;i++)
	{
		map<string,int> m;
		for(j=0;j+i<len;j++)
		{
			ss[0]=s[j];ss[1]=s[j+i];//printf("%s\n",ss);
			if(m[ss]==1)
			return 0;
			else
			m[ss]++;
		}
	}
	return 1;
}
int main(void)
{
	int i,j,k,m,n;
	char s[1000];
	while(scanf("%s",s),s[0]!='*')
	{
		if(Ma(s))
		printf("%s is surprising.\n",s);
		else
		printf("%s is NOT surprising.\n",s);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44584292/article/details/102959156