A - Surprising Strings
题意就是给你一个字符串,例如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<iostream>//暴力
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
const int mod=100007;
using namespace std;
char s[100];
int ans(){
int l,d,i,j;
l=strlen(s);
if(l<3)
return 1;
d=1;
while(d<l){
for(i=0;i<l-d;i++){
for(j=i+1;j<l-d;j++){
if(s[i]==s[j]&&s[i+d]==s[j+d])
return 0;
}
}
d++;
}
return 1;
}
int main(){
while(scanf("%s",s)){
if(s[0]=='*')
return 0;
if(ans())
printf("%s is surprising.\n");
else
printf("%s is NOT surprising.\n");
}
return 0;
}
#include<iostream>
#include<string>
#include<map>
#include<cstdio>
#include<stdio.h>
#include<string.h>
using namespace std;
const int kMaxn(2007);
int main()
{
char str[10005],pair[3];
map<string,int> r;
while(scanf("%s",str) != EOF)
{
if(str[0] == '*')
break;
int leap,i,len,j;
len = strlen(str);
for(i = 0;i < len;i++)
{
leap = 1;
r.clear();
for(j = 0;j < len-i-1;j++)
{
pair[0] = str[j];
pair[1] = str[j+i+1];
pair[2] = '\0';
r[pair]++;
if(r[pair] > 1)
leap = 0;
}
if(leap == 0)
break;
}
if(leap)
printf("%s is surprising.\n",str);
else
printf("%s is NOT surprising.\n",str);
}
return 0;
}

京公网安备 11010502036488号