TOJ 2522 水~ Surprising Strings

题目来源: http://acm.tju.edu.cn/toj/showp.php?pid=2522

看了半天题意== ,还在网上搜了才知道意思==

大致意思如下:

D pairs 是指 字符串中相邻为D的字母对, D-Unique 是指所有的Dpair 各不相同;即可;

比如 ZGBG ,  其中相邻为0  有: ZG , GB ,     BG 各不同,所以是0-unique; 距离为1的有, ZB, GG 不同所以为1-unique; 相邻为2只有ZG 也是2-Unique; 

用hash 或者map 映射一下,就可以;当然也可以暴力搜索;

下面是map 和hash 的代码:

#include <iostream>
#include <string>
#include <map>
using namespace std;
int DPair(string str)
{
    for(int D=0;D<str.length()-1;D++)
    {
        map<string ,int> mymap;
        string ss="AA";
       for(int i=0;i<str.length()-D-1;i++)
       {
           ss[0]=str[i];ss[1]=str[i+D+1];
           if(mymap[ss]==1) return 0;
           mymap[ss]=1;
       }
    }
    return 1;
}
int main()
{
    string str;
    while(cin>>str)
    {
        i
        f(!str.compare("*")) break;
        if(DPair(str))
        cout<<str<<" is surprising."<<endl;
        else cout<<str<<" is NOT surprising."<<endl;
    }
}
hash 代码如下:
#include <iostream>
#include <string.h>
using namespace std;
int hash[2500];
int main()
{
    char str[85];
    while(cin>>str)
    {
        if(!strcmp(str,"*")) break;
        int d=0,len=strlen(str),flag=1;
        while(d<len&&flag)
        {
            memset(hash,0,sizeof(hash));
            for(int i=0; i+d+1<len&&flag; i++)
            {
                int t=(str[i]-'0')*26+str[i+d+1]-'0';
                if(!hash[t])hash[t]=1;
                else flag=0;
            }
            d++;
        }
        if(flag) cout<<str<<" is surprising.\n";
        else cout<<str<<" is NOT surprising.\n";
    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值