10945 - Mother bear

Moderator: Board moderators

aur
New poster
Posts: 1
Joined: Fri Aug 12, 2011 4:00 pm

Re: 10945 - Mother Bear

I have been getting a weird WA

here's my code :

Code: Select all

``````#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

int main()
{

string s;
while(getline(cin,s)){
if(s=="DONE")
break;
for(int i=0;i<s.length();i++)
if(s[i] == ' ' || s[i]== '.' || s[i]==',' || s[i]=='!' || s[i]=='?')
s.erase(i,1);

for(int i=0;i<s.length();i++)
if(s[i] == ' ')
s.erase(i,1);

string::reverse_iterator rit;
int i=0,counter=0;
for ( rit=s.rbegin() ; rit < s.rend(); rit++,i++ ){
if(*rit == s[i] || *rit+32==s[i] || *rit-32==s[i])
counter++;
}

if(counter==s.length())
cout<<"You won't be eaten!"<<endl;
else cout<<"Uh oh.."<<endl;

}

return 0;
}
``````

New poster
Posts: 2
Joined: Fri Aug 05, 2011 9:35 pm

Re: 10945 - Mother Bear

I don't know why I got WA, plz Help Me...I am new in UVa Online Judge

Here is My Code,

#include<iostream>
#include<cmath>;
#include<vector>
using namespace std;
int main()
{
string S;
int Characters=0,Punc=0;
int X,i,M=0;
vector<string>Strs;

while(getline(cin,S)&&S!="DONE")
{
Strs.push_back(S);
}
while(M<Strs.size())
{
Characters=0; Punc=0;
S=Strs[M];
X=S.size()-1;
for(i=0;i<=ceil((double)S.size()/2);i++)
{
if(S.size()%2==0 && i==ceil((double)S.size()/2)&&Punc==0) break;
if(X == ceil((double)S.size()/2)-1&& i==ceil((double)S.size()/2)-1)
{Punc++;break;}

if(S == '.'||S == ','||S == '?'||S == '!'||S == ' ')
{
Punc++;
continue;}

if(S[X] == '.'||S[X] == ','||S[X] == '?'||S[X] == '!'||S[X] == ' ')
{
i=i-1;
X--;
Punc++;
continue;
}
if(S == toupper(S[X]) || S == tolower(S[X]))
Characters++;
X--;
}

if(Characters*2+Punc == S.size())
{cout<<"You won't be eaten!"; if(M!=Strs.size()-1) cout<<endl;}
else{ cout<<"Uh oh.."; if(M!=Strs.size()-1) cout<<endl;}
M++;
}
// system("pause");
return 0;
}

Thanks,

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

10945 - Mother Bear WA why? Anyone help me please

Actually, I have seen the previous posted topic of this problem and try almost all of the given test cases and my program gives the right. But, in judge verdict give me WA.

Code: Select all

``````#include<cstdio>
#include<sstream>
#include<cctype>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<iterator>
using namespace std;

int main()
{
char in[1000000];
while(gets(in))
{
if(in[0]!='\0')
{
if( (in[0]=='D' && in[1]=='O' && in[2]=='N' && in[3]=='E' && in[4]=='\0')) break;
char store[1000000],rev[1000000];
unsigned long I=0, len=strlen(in);
int p=0, fag=0;
for(unsigned long i=0 ; i<=len ; i++)
{
if(isalpha(in[i]))
{
store[I]=tolower(in[i]);
I++;
}
}
for(unsigned long i=0 ; i<I ; i++)
{
rev[i]=store[I-1-i];
}

for(unsigned long i=0 ; i<I ; i++)
{
fag=1;
if(store[i]!=rev[i])
{
p=1;
break;
}
}
if(fag==1)
{
if(p==0)
cout<<"You won't be eaten!"<<endl;
else
cout<<"Uh oh.."<<endl;

p=0; fag=0;
for(unsigned long i=0 ; i<I ; i++)
{
store[i]=rev[i]='\0';
}
}
}
else if(in[0]=='\0') cout<<"You won't be eaten!"<<endl;
}

return 0;
}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10945 - Mother Bear WA why? Anyone help me please

My AC code prints "You won't be eaten!" for a line containing only a space. Yours prints nothing.
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

Re: 10945 - Mother Bear WA why? Anyone help me please

Still WA please give more test cases....

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

Re: 10945 - Mother Bear WA why? Anyone help me please

Hi, guys what is it you post?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10945 - Mother Bear WA why? Anyone help me please

Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

Re: 10945 - Mother Bear WA why? Anyone help me please

Here, is my updated code:

Code: Select all

``````#include<cstdio>
#include<sstream>
#include<cctype>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<iterator>
using namespace std;

int main()
{
char in[1000000];
while(gets(in))
{
if(in[0]!='\0' && in[0]!=' ')
{
if( (in[0]=='D' && in[1]=='O' && in[2]=='N' && in[3]=='E' && in[4]=='\0')) break;
char store[1000000],rev[1000000];
unsigned long I=0, len=strlen(in);
int p=0, fag=0;
for(unsigned long i=0 ; i<=len ; i++)
{
if(isalpha(in[i]))
{
store[I]=tolower(in[i]);
I++;
}
}
for(unsigned long i=0 ; i<I ; i++)
{
rev[i]=store[I-1-i];
}

for(unsigned long i=0 ; i<I ; i++)
{
fag=1;
if(store[i]!=rev[i])
{
p=1;
break;
}
}
if(fag==1)
{
if(p==0)
cout<<"You won't be eaten!"<<endl;
else
cout<<"Uh oh.."<<endl;

p=0; fag=0;
for(unsigned long i=0 ; i<I ; i++)
{
store[i]=rev[i]='\0';
}
}
}
else if(in[0]=='\0' || in[0]==' ') cout<<"You won't be eaten!"<<endl;
}

return 0;
}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10945 - Mother Bear WA why? Anyone help me please

Try the input _a_b_c with the underscores as spaces.
It should print Uh oh..
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

Re: 10945 - Mother Bear WA why? Anyone help me please

Hi, brother
Thank you, Thank you, Thank you
Thank you very much
Thanks a lot...

sumit saha shawon
New poster
Posts: 19
Joined: Tue Jun 26, 2012 9:19 pm

Re: 10945 - Mother Bear WA why? Anyone help me please

what is my problem?All of my input and output is ok.But I am getting WA.
my code is:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[3000],str1[3000],str2[30000];
while(gets(str))
{
int i,j,k,len;
if(strcmp(str,"DONE")==0)
break;
len=strlen(str);
for(i=0; i<len; i++)
str=toupper(str);
k=0;
for(i=0;i<len; i++)
if(str>='A' && str<='Z')
str2[k++]=str;
i=0;
for(j=k-1; j>=0; j--)
str1[i++]=str2[j];
// puts(str1);
//puts(str2);
if(strcmp(str1,str2)==0)
printf("You won't be eaten!\n");
else
printf("Uh oh..\n");
}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10945 - Mother Bear WA why? Anyone help me please

Null terminate the strings before you run strcmp()
Check input and AC output for thousands of problems on uDebug!

sumit saha shawon
New poster
Posts: 19
Joined: Tue Jun 26, 2012 9:19 pm

Re: 10945 - Mother Bear WA why? Anyone help me please

thanks I have got AC

Kashmir
New poster
Posts: 1
Joined: Sun Oct 07, 2012 2:51 am

Re: 10945 - Mother Bear WA why? Anyone help me please

Hello everyone!
Well, I've got a problem with this. I keep getting WA, and I can't find what's wrong.
Can anybody help me?
This is my code:

Thanks.

Code: Select all

``````#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define REG 10
#define LEN 10
#define ES_PALINDROMO "You won't be eaten!"
#define NO_ES_PALINDROMO "Uh oh.."

int caracterValido( char ** s , int i );

void main()
{
char string[REG][LEN], * fin, * s;
int i = 0, j;

fflush( stdin );

if( !gets( string[i] ) )
{
exit(1);
}

while( strcmp( string[i] , "DONE") != 0 && i < REG-1 && strlen( string[i] ) < LEN )
{
fflush( stdin );

i++;

if( !gets( string[i] ) )
{
break;
}

}

for( j = 0 ; j < i ; j++ )
{
s = string[j];

fin = strrchr( string[j] , '\0' ) - 1;

while( caracterValido( &s , 1 ) && caracterValido( &fin , -1 ) && tolower(*s) == tolower(*fin) && ( s < fin ) )
{
s++;
fin--;
}

if( s >= fin && *s != '\0'  )
puts( ES_PALINDROMO );
else
puts( NO_ES_PALINDROMO );
}

}

int caracterValido( char ** s , int i )
{
while ( (tolower(**s) >= 'a' && **s <= 'z') || **s == '.' || **s == ',' || **s == '!' || **s == '?' || **s == ' ')
{
if( !(tolower(**s) >= 'a' && **s <= 'z') )
*s += i;
else
return 1;

}

return 0;

}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10945 - Mother Bear WA why? Anyone help me please

This isn't printing anything. Don't use fflush(stdin). main should return 0 on completion.
Check input and AC output for thousands of problems on uDebug!