384 - Slurpys
Posted: Sat Sep 06, 2003 4:41 pm
I have tried all cases that I can think about, who can give me more testcases?
this is my code:
[cpp]#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string reverse(string t){
int length=t.length(),i;
for(i=0;i<length/2;i++)
swap(t,t[length-1-i]);
return t;
}
bool slump(string t){
int length=t.length(),pos;
if(length==0 || t[0]!='D' && t[0]!='E')
return false;
if(length==1 || t[1]!='F')
return false;
pos=1;
while(pos<length && t[pos]=='F')
pos++;
if(pos==length)
return false;
else
return (t[pos]=='G' && pos==length-1 || slump(t.substr(pos)));
}
bool slimp(string t){
int length=t.length();
if(t[0]!='A' || length<=1)
return false;
if(length==2)
return t[1]=='H';
else if(t[length-1]=='C' && slump(t.substr(1,length-2)) || t[1]=='B' && t[length-1]=='C' && slimp(t.substr(2,length-3)))
return true;
else
return false;
}
int main(){
int n,i,length,pos,posh,posc;
cin>>n;
string t,reverse_t;
cout<<"SLURPYS OUTPUT\n";
for(i=1;i<=n;i++){
cin>>t;
length=t.length();
reverse_t=reverse(t);
posh=reverse_t.find('H');
posc=reverse_t.find('C');
if(posh==string::npos && posc==string::npos){
cout<<"NO\n";
continue;
}
else if(posh==string::npos)
pos=length-1-posc;
else if(posc=string::npos)
pos=length-1-posh;
else
pos=length-1-min(posc,posh);
if(slimp(t.substr(0,pos+1)) && pos+1<=length-2 && slump(t.substr(pos+1)))
cout<<"YES";
else
cout<<"NO";
cout<<endl;
}
cout<<"END OF OUTPUT\n";
return 0;
}[/cpp]
this is my code:
[cpp]#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string reverse(string t){
int length=t.length(),i;
for(i=0;i<length/2;i++)
swap(t,t[length-1-i]);
return t;
}
bool slump(string t){
int length=t.length(),pos;
if(length==0 || t[0]!='D' && t[0]!='E')
return false;
if(length==1 || t[1]!='F')
return false;
pos=1;
while(pos<length && t[pos]=='F')
pos++;
if(pos==length)
return false;
else
return (t[pos]=='G' && pos==length-1 || slump(t.substr(pos)));
}
bool slimp(string t){
int length=t.length();
if(t[0]!='A' || length<=1)
return false;
if(length==2)
return t[1]=='H';
else if(t[length-1]=='C' && slump(t.substr(1,length-2)) || t[1]=='B' && t[length-1]=='C' && slimp(t.substr(2,length-3)))
return true;
else
return false;
}
int main(){
int n,i,length,pos,posh,posc;
cin>>n;
string t,reverse_t;
cout<<"SLURPYS OUTPUT\n";
for(i=1;i<=n;i++){
cin>>t;
length=t.length();
reverse_t=reverse(t);
posh=reverse_t.find('H');
posc=reverse_t.find('C');
if(posh==string::npos && posc==string::npos){
cout<<"NO\n";
continue;
}
else if(posh==string::npos)
pos=length-1-posc;
else if(posc=string::npos)
pos=length-1-posh;
else
pos=length-1-min(posc,posh);
if(slimp(t.substr(0,pos+1)) && pos+1<=length-2 && slump(t.substr(pos+1)))
cout<<"YES";
else
cout<<"NO";
cout<<endl;
}
cout<<"END OF OUTPUT\n";
return 0;
}[/cpp]