您的位置:首页 > 其它

POJ 3096-Surprising Strings(map-相同串)

2017-04-10 13:47 525 查看
Surprising Strings

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

Mid-Central USA 2006

题目意思:

任意给一串字符,例如ZGBG,从中取两个字符生成新的串。D-pairs中的D指的是当前取的第一个字符与上一个串的第一个字符之间相隔的字符的个数,D-unique指的是这些D-pairs字符串是否互不相同。
对于 0-pairs ZG, GB, BG,即Z、G、B连续相隔字符为0,0-unique 即表示ZG, GB, BG互不相同,依次类推…

解题思路:

D的范围是0~len-1,D-pairs的首字符起始位置范围是0~len-D。
在不同间隔下用map处理判断,一旦有相同的串则非D-unique。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息