第二道map的应用
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17090
题意就是给你一个字符串,例如ZGBG,有一种称谓叫D-unique
这个字符串 在D=0时, 有三个子串 ZG GB BG,因为这三个都不同,也就是unique,所以ZGGB是 0-unique;
同理 D=1时,有两个子串ZB GG也是不同,所以ZGGB是 1-unique;
D=2时,只有一个子串ZG,独一无二,所以ZGGB是 2-unique;
ince these three pairs are all different,Thus ZGBG is surprising.因为这三个都是独一无二的,所以满足题目要求;
用map建立每个子串与数字的联系来统计,真的很方便啊。
上代码:
#include <map>
#include <iostream>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int sign=0;
map<string,int>mp;
if(s[0]=='*')
break;
int len=s.length();
for(int i=0;i<=len-2;i++)
{
for(int j=0;j+(i+1)<len;j++)
{
string s2="";
s2+=s[j];
s2+=s[j+(i+1)];
if(!mp.count(s2))
mp[s2]=0;
mp[s2]++;
if(mp[s2]==2)//如果发现子串出现了两次,就跳出循环
{
sign=1;
break;
}
}
if(sign)
break;
mp.clear();//每组统计完清空map
}
if(!sign)
cout<<s<<" is surprising.\n";
else
cout<<s<<" is NOT surprising.\n";
}
return 0;
}