HDOJ 2736 Surprising Strings

时间:2022-02-18 13:30:36

Surprising Strings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 294    Accepted Submission(s): 209

Problem Description
The D-pairs of a string of letters are the ordered pairs of letters that are distance D from each other. A string is D-unique if all of its D-pairs are different. A string is surprising if it is D-unique for every possible distance D.

Consider the string ZGBG. Its 0-pairs are ZG, GB, and BG. Since these three pairs are all different, ZGBG is 0-unique. Similarly, the 1-pairs of ZGBG are ZB and GG, and since these two pairs are different, ZGBG is 1-unique. Finally, the only 2-pair of ZGBG is ZG, so ZGBG is 2-unique. Thus ZGBG is surprising. (Note that the fact that ZG is both a 0-pair and a 2-pair of ZGBG is irrelevant, because 0 and 2 are different distances.)

Acknowledgement: This problem is inspired by the "Puzzling Adventures" column in the December 2003 issue of Scientific American.

Input
The input consists of one or more nonempty strings of at most 79 uppercase letters, each string on a line by itself, followed by a line containing only an asterisk that signals the end of the input.
Output
For each string of letters, output whether or not it is surprising using the exact output format shown below.
Sample Input
ZGBG
X
EE
AAB
AABA
AABB
BCBABCC
*
Sample Output
ZGBG is surprising.
X is surprising.
EE is surprising.
AAB is surprising.
AABA is surprising.
AABB is NOT surprising.
BCBABCC is NOT surprising.

自己写的代码一直wa....求高手

#include <iostream>
#include <string>
#include <string.h>
#include <cctype>
using namespace std;
///**wa代码**/
bool judge(string a,string b)
{
for(int i=0; i<a.length(); i++)
{
for(int j=i+1; j<a.length(); j++)
{
if(a[i]==a[j] && b[i]==b[j])
{
return true;
}
}
}
return false;
}
int main()
{
string s="";
while(cin>>s && s[0] != '*')
{
string str,ans1,ans2,table1,table2,count1,count2;
str=ans1=ans2=table1=table2=count1=count2="";
int k=0;
str=s;
for(int i=0; i<s.length(); i++)
{
if(islower(s[i]))
s[i]=toupper(s[i]);
else
s[i]=s[i];
}
// cout<<s<<" "<<str<<endl;
if(s.length()>1)
{
for(int i=0; i<s.length()-1; i++)
{
ans1+=s[i];
ans2+=s[i+1];
}
}
if(s.length()>2)
{
for(int i=0; i<s.length()-2; i++)
{
table1+=s[i];
table2+=s[i+2];
}
}
if(s.length()>3)
{
for(int i=0; i<s.length()-3; i++)
{
count1+=s[i];
count2+=s[i+3];
}
} if(!judge(ans1,ans2) && !judge(table1,table2) &&!judge(count1,count2))
cout<<str<<" is surprising."<<endl;
else
cout<<str<<" is NOT surprising."<<endl;
/*cout<<ans1<<" ";
cout<<ans2<<" ";
cout<<table1<<" ";
cout<<table2<<" "<<count1<<" "<<count2<<endl;*/
}
} /**别人正确代码**/
#include<iostream>
#include<string>
using namespace std;
int map[26][26];
char str[10000];
int change(char a)
{
return (int)(a-'A');
}
int main()
{
int i,j;
while(cin>>str&&str[0]!='*')
{
int len=strlen(str);
int flag=0;
for(i=0; i<=len-2; i++)
{
memset(map,0,sizeof(map));
for(j=0; j<=len-2-i; j++)
{
int x=change(str[j]);
int y=change(str[j+i+1]);
if(map[x][y])
{
flag=1;
break;
}
else map[x][y]=1;
}
if(flag==1) break;
}
if(flag) cout<<str<<" is NOT surprising."<<endl;
else cout<<str<<" is surprising."<<endl; }
return 0;
}