| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7072 | Accepted: 4577 |
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.
Source
题目意思:
解题思路:
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0xfffffff
#define MAXN 100
string c;
map<string,bool> ma;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("G:/cbx/read.txt","r",stdin);
//freopen("G:/cbx/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>c)
{
if(c[0]=='*') break;
int len=c.size();
bool flag;
for(int k=1; k<len; ++k) //间隔
{
ma.clear();//清空map
flag=false;//是否NOT surprising
int cnt=0;//串的个数
for(int i=0; i<len-k; ++i)//起始位置
{
string s="";
s+=c[i];
s+=c[i+k];
ma[s]=true;//记录
++cnt;
//cout<<s<<endl;
}
if(ma.size()!=cnt)//map中的串的数目与串的总数不同
{
flag=true;
break;
}
}
if(flag) cout<<c<<" is NOT surprising."<<endl;
else cout<<c<<" is surprising."<<endl;
}
return 0;
}
本文介绍了一种用于判断字符串是否为SurprisingStrings的算法,SurprisingStrings是一种特殊字符串,其所有距离D的字母对都不相同。文章详细解析了输入输出格式及样例,并提供了完整的C++实现代码。
403

被折叠的 条评论
为什么被折叠?



