转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1307434869
大致题意:
定义D-pairs表示取字符串s中相距为D的两个字母所构成的字母对,该字母对中两个字母的位置顺序与他们在主串s中的位置顺序一致
定义D-unique表示,若从字符串s中取出所有相距为D的字母对D-pairs,且这些D-pairs都是独一无二的,那么成字符串s是一个D-unique串
D的取值范围为0~s.len()-2
假如字符串s对于所有的D都有D-unique成立,则字符串s是令人惊讶的 = =
现在输入一些字符串,问他们能不能令人惊讶= =
解题思路:
令人惊讶的中级水题= =
用STL的map标记D-unique是否重复出现就OK了
也可以用ASCII标记,取两个大写字母的ASCII构成一个四位数作为key就可以了,比map快一点点
层次关系:
对于某个D,当所有D-pairs都不同时,s是D-unique
对于所有D,s都有D-unique时,它是surprising string
注意,长度小于等于2的s都是surprising string
其实我感觉这题暴力也能AC= =,S最大长度也就79...
1 /*STL<map>标记*/
2
3 //Memory Time
4 //212K 16MS
5
6 #include<iostream>
7 #include<string>
8 #include<map>
9 using namespace std;
10
11 int main(void)
12 {
13 char s[80];
14 while(cin>>s && s[0]!='*')
15 {
16 int len=strlen(s);
17 if(len<=2) //长度小于等于2的串必定是surprising String
18 {
19 cout<<s<<" is surprising."<<endl;
20 continue;
21 }
22
23 bool mark=true; //标记s是否为Surprising String
24 for(int d=0;d<=len-2;d++) //d为当前所选取的两个字母之间的距离,d(max)=len-2
25 {
26 map<string,bool>flag;
27
28 bool sign=true; //标记D-pairs字母对是不是D-unique
29 for(int i=0;i<=len-d-2;i++) //i为所选取的两个字母中第一个字母的下标
30 {
31 char pair[3]={s[i],s[i+d+1],'\0'}; //构成D-pairs字母对
32
33 if(!flag[ pair ])
34 flag[ pair ]=true;
35 else
36 {
37 sign=false; //存在相同的D-pairs,该字母对不是D-unique
38 break;
39 }
40 }
41 if(!sign)
42 {
43 mark=false; //存在非D-unique,s不是Surprising String
44 break;
45 }
46 }
47 if(mark)
48 cout<<s<<" is surprising."<<endl;
49 else
50 cout<<s<<" is NOT surprising."<<endl;
51 }
52 return 0;
53 }
========华丽的分割线=======
1 /*ASCII标记*/
2
3 //Memory Time
4 //212K 0MS
5
6 #include<iostream>
7 #include<string>
8 using namespace std;
9
10 int main(void)
11 {
12 char s[80];
13 while(cin>>s && s[0]!='*')
14 {
15 int len=strlen(s);
16 if(len<=2) //长度小于等于2的串必定是surprising String
17 {
18 cout<<s<<" is surprising."<<endl;
19 continue;
20 }
21 bool mark=true; //标记s是否为Surprising String
22 for(int d=0;d<=len-2;d++) //d为当前所选取的两个字母之间的距离,d(max)=len-2
23 {
24 bool flag['Z'*100+'Z'+1]; //标记D-pairs字母对
25 memset(flag,false,sizeof(flag));
26
27 bool sign=true; //标记D-pairs字母对是不是D-unique
28 for(int i=0;i<=len-d-2;i++) //i为所选取的两个字母中第一个字母的下标
29 {
30 int pair=s[i]*100+s[i+d+1]; //D-pairs字母对的ASCII码所构成的四位数
31
32 if(!flag[pair])
33 flag[pair]=true;
34 else
35 {
36 sign=false; //存在相同的D-pairs,该字母对不是D-unique
37 break;
38 }
39 }
40 if(!sign)
41 {
42 mark=false; //存在非D-unique,s不是Surprising String
43 break;
44 }
45 }
46 if(mark)
47 cout<<s<<" is surprising."<<endl;
48 else
49 cout<<s<<" is NOT surprising."<<endl;
50 }
51 return 0;
52 }