题目:Surprising Strings
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
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.
题目大意:
输入一串字符串,输出他是“否令人惊讶”(输入以“*”结束)
“是否令人惊讶”的判断规则为:
保证每对相距k距离的字母不同k小于输入的字符串长度
比如ZGBG
相距0距离的字母对为ZG,GB,BG都不同;
相距1距离的字母对为ZB,GG都不同;
相距2距离的字母对为ZG;
则他为令人惊讶的;
比如AABB
相距0----AA,AB,BB
相距1----AB,AB相同了
相距2----AB
则他为不令人惊讶的
题目思路:
如果按题目要求来判断是有点复杂的最后可能还会超时
所以应该想办法转化题目
们可以让每两个相同的字母为一对
判断两对字母对之间的距离是否相同,有距离相同的即不令人惊讶了
比如AABB
AA相同为一对距离为0,BB相同为一对距离为0,这两对距离相同不令人惊讶
比如BCBABCC相同的字母对和距离如下
B-B 1
B—B 3
C—C 3
C----C 4
可以看出来有两对字母对的距离相同,所以不令人惊讶
AC代码:
#include <iostream>
#include<string>
#include<set>
using namespace std;
int b[100];
int main()
{
string a,word;
while(cin>>a)
{
if(a=="*")
break;
int k=0;
for(int i=0; i<a.size()-1; i++)
{
for(int j=i+1; j<a.size(); j++)
{
if(a[i]==a[j])
b[k++]=j-i;
}
}
set<int> distance;
for(int i=0; i<k; i++)
{
distance.insert(b[i]);
}
int temp=distance.size();
if(temp==k)
cout<<a<<" is surprising."<<endl;
else
cout<<a<<" is NOT surprising."<<endl;
}
return 0;
}