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

本文介绍了一种用于判断字符串是否为SurprisingStrings的算法,SurprisingStrings是一种特殊字符串,其所有距离D的字母对都不相同。文章详细解析了输入输出格式及样例,并提供了完整的C++实现代码。
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


题目意思:

任意给一串字符,例如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;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值