题目来源: 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";
}
}