Surprising Strings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 344 Accepted Submission(s): 243
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.
题意:给你一个字符串,判断是不是个surprising 串,举例说明 (其中D-pairs意思为一对字母的距离为D)
例如 :ZGBG 如0-pairs—>(ZG,GB,BG) 当D为0时,符合 surprising 串 的规则。
1-paris—>(ZB, GG) 当D为1时,符合 surprising 串的规则。
2-paris—>(ZG) 当D为2时,符合 surprising 串的规则。
此时应该注意,D(max) 应该为 strlen(S串)- 2 ;
只有当D取到最大值的时候,所有情况都满足surprising 串的规则,S串才是 一个 surprising 串,否则为 NOT。
这个题是个字符串处理的问题 ,所以总结一下,处理此种问题的思路,因为思路才是最重要的!。
#include<stdio.h>
#include<string>
#include<string.h>
#include<map>
#include<iostream>
using namespace std;
int main()
{
int i,d, mark = 0;
char b[100];
map<string, bool>flag;
while(gets(b)!=NULL && b[0]!='*')
{
int len = strlen(b);
mark =0;
for(d=0; d<=len-2; d++)
{
flag.clear();
for(i=0; i<=len-d-2; i++)
{
char a[3]={b[i],b[i+d+1],'\0'};
if(!flag[a])
flag[a] = 1;
else
{
mark = 1;
printf("%s is NOT surprising.\n", b);
break;
}
}
if(mark)
{
break;
}
}
if(!mark)
printf("%s is surprising.\n", b);
}
return 0;
}