10945 - Mother bear

All about problems in Volume 109. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Fri Nov 23, 2007 8:47 pm

I have sent you a PM.
Ami ekhono shopno dekhi...
HomePage

turcse143
Learning poster
Posts: 81
Joined: Wed May 09, 2007 9:59 pm
Location: (CSE,DU) Dhaka,Bangladesh

Post by turcse143 » Thu Feb 28, 2008 6:28 pm

I think there is a problem in here check it out.

Code: Select all

for(p=0,q=strlen(pal)-1;p<strlen(pal),q>=0;p++,q--)
      {
         if(pal[p]==pal[q])
            flag++;
         else
         {
            flag=-1;
            break;
         }
      } 
---take array size 50000.
---u skip other character
---build an array
---divided the array
--- compare if it is palindrom.
hope it helps
''I want to be most laziest person in the world''

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Kidding CE

Post by Obaida » Wed Jul 02, 2008 11:15 am

I can't understand why i have compilation error!!! :o
What is wrong with this??? Why this kidding with me???

Code: Select all

Done....
Last edited by Obaida on Mon Dec 15, 2008 9:32 am, edited 1 time in total.
try_try_try_try_&&&_try@try.com
This may be the address of success.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 10945 - Mother Bear

Post by Jan » Wed Jul 02, 2008 3:35 pm

You can't use strrev(). Build it manually.
Ami ekhono shopno dekhi...
HomePage

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10945 - Mother Bear

Post by Obaida » Wed Jul 02, 2008 3:41 pm

Thanks a lot Vai.
Thanks a lot again for.....( not helping me):oops:
Do you know how much stupid i am?
I know!!! :wink:
try_try_try_try_&&&_try@try.com
This may be the address of success.

empo
New poster
Posts: 19
Joined: Mon Jul 28, 2008 7:00 pm
Location: India

Re: 10945 - Mother Bear

Post by empo » Wed Dec 10, 2008 11:40 am

I dont think there us any input like

Code: Select all

empoopme
done
DONE
because i got accepted and using tolower() for every char input and at last comparing my string with "done" not "DONE"..

Ouput i am having is
You won't be eaten!

NOTTTTTTTT
You won't be eaten!
Uh oh..

This means program an be break at done or DONE..
"Accepted" is my passion but RTE is my weakness.....

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

10945 - run time error Mother Bear

Post by sazzadcsedu » Thu Jan 01, 2009 10:54 pm

whats wrong??
why runtime error??

Code: Select all


            #include<stdio.h>
            #include<string.h>
            #include<memory.h> 


                         int  main()

						   {
                               int i,j,l,flag;
							   char str[10000],a,onlychar[10000],reonlychar[10000];
							   char words[52]={'a','b','c','d','e','f','g','h','i','j',
								 'k','l','m','n','o','p','q','r','s','t','u','v','w','x',
							  'y','z','A','B','C','D','E','F','G','H','I','J','K',
							'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
                              int array1[10000],array2[10000];
                        
						while(gets(str))
							
						{


                                memset(array1,0,sizeof(array1));
								memset(array2,0,sizeof(array2));


                   l=0;
				   j=0;
				   flag=1;
				                     if (!strcmp(str,"DONE"))
						  break;

                 
					for(i=0;i<=strlen(str)-1;i++)
					{ 
					   a=str[i];
                      
					   for(j=0;j<52;j++)
                          
					   {  
						   if(a==words[j])
						   {
							  
							   array1[l]=j%26;
                             onlychar[l]=a;
                              l++;
							
                             break;
						   }

                       }

					}   
					     onlychar[l]='\0';

                     j=0;
				
					  
						 
						 
						 
						 for(i=l-1;i>=0;i--)
					   { 
						   a=onlychar[i];
						   //printf("%c",a);
                           reonlychar[j]=a;
                       
                           j++;  
                           
					   }  
                      reonlychar[j]='\0';
 
                     l=0;

	               for(i=0;i<=strlen(reonlychar)-1;i++)
					{ 
					   a=reonlychar[i];
                      
					   for(j=0;j<52;j++)
                          
					   {  
						   if(a==words[j])
						   {
							   
							   array2[l]=j%26;
                             onlychar[l]=a;
                              l++;
							  
                             break;
						   }

                       }

					}   

                              










					      //printf("%d",j);
					     

						  for(i=0;reonlychar[i];i++)
						  {
 
                             //printf("%c",reonlychar[i]);

						  }
                             // printf("\n");
 
                              for(i=0;onlychar[i];i++)

							  {  

                                     

									  if(array1[i]!=array2[i])
									  { 
                                      
                                  
									     flag=0; 
									  }
								  
								       if(flag==0)

                                         break;
							   }    
                             
							    if(flag==0)
                                printf("Uh oh..\n");
								else
                                printf("You won't be eaten!\n");

                      


								}

								return 0;
						   }

Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10945 - Mother Bear

Post by Obaida » Sat Jan 03, 2009 6:40 am

Check your array size it's a bit small, Causing RTE. :wink:
try_try_try_try_&&&_try@try.com
This may be the address of success.

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

10945 - Mother Bear

Post by sazzadcsedu » Thu Jan 29, 2009 10:44 pm

why RTE??
MY code

Code: Select all

  #include<stdio.h>
            #include<string.h>
            #include<memory.h> 


                  char str[200000],a,onlychar[200000],reonlychar[200000];
                  int array1[200000],array2[200000];

                         int  main()

						   {
                                int i,j,l,flag;
							   
							   char words[52]={'a','b','c','d','e','f','g','h','i','j',
								 'k','l','m','n','o','p','q','r','s','t','u','v','w','x',
							  'y','z','A','B','C','D','E','F','G','H','I','J','K',
							'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
                    
							   
							   // freopen("input.txt","r",stdin);
							   // freopen("output.txt","w",stdout);
						

                        
						while(gets(str))
							
						{


                                memset(array1,0,sizeof(array1));
								memset(array2,0,sizeof(array2));


                   l=0;
				   j=0;
				   flag=1;
				
             
 
			

                      if (!strcmp(str,"DONE"))
						  break;

                 
					for(i=0;i<=strlen(str)-1;i++)
					{ 
					   a=str[i];
                      
					   for(j=0;j<52;j++)
                          
					   {  
						   if(a==words[j])
						   {
							  
							   array1[l]=j%26;
                             onlychar[l]=a;
                              l++;
							  //printf("%c",words[j]);
                             break;
						   }

                       }

					}   
					     onlychar[l]='\0';

                     j=0;
						 //printf(onlychar);
						 //printf("\n");
					//printf("%d\n",strlen(onlychar));
						 //printf("%d",l);
					  
						 
						 
						 
						 for(i=l-1;i>=0;i--)
					   { 
						   a=onlychar[i];
						   //printf("%c",a);
                           reonlychar[j]=a;
                           //printf("%c",reonlychar[j]);
                           j++;  
                           
					   }  
                      reonlychar[j]='\0';
 
                     l=0;

	               for(i=0;i<=strlen(reonlychar)-1;i++)
					{ 
					   a=reonlychar[i];
                      
					   for(j=0;j<52;j++)
                          
					   {  
						   if(a==words[j])
						   {
							   
							   array2[l]=j%26;
                             onlychar[l]=a;
                              l++;
							  //printf("%c",words[j]);
                             break;
						   }

                       }

					}   

                              










					      //printf("%d",j);
					  
                         
 
                              for(i=0;onlychar[i];i++)

							  {  

                                     
                                     

									  if(array1[i]!=array2[i])
									  { 
                                  
                                   
									     flag=0; 
									  }
								  
								       if(flag==0)

                                         break;
							   }    
                             
							    if(flag==0)
                                printf("Uh oh..\n");
								else
                                printf("You won't be eaten!\n");

								}

								return 0;
						   }

Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

newkid
Learning poster
Posts: 73
Joined: Fri Dec 12, 2008 3:06 am

Re: 10945 - Mother Bear

Post by newkid » Fri Jan 30, 2009 9:10 pm

@sazzadcsedu

man.. your code indentation is horrible.. can you edit your post with proper indentation?
hmm..

sayed
New poster
Posts: 2
Joined: Tue Sep 01, 2009 11:47 am
Location: Bangladesh
Contact:

mother bear

Post by sayed » Tue Sep 01, 2009 12:44 pm

i cannot finding the solution for a long long time .I am getting wrong answer for many times.
here is my code any body help me..

# include <stdio.h>
# include<string.h>
# include<ctype.h>


int a,b,i,j,flag,len,k;
char ch[100],kh[100];

int main()
{
int p;
while(gets(ch)!=NULL)
{
if(!strcmp(ch,"DONE"))
break;
for(i=0;ch;i++)
if (ch>='A' && ch<='Z')
ch=ch+32;

for(i=0,j=0;ch;i++)
if (isalpha(ch))
kh[j++]=ch;

for(i=j-1,k=0;i>=0;i--)
ch[k++]=kh;

flag=0;
for(i=0;i<k;i++)
{
if(ch!=kh[i])
{
flag=0;
break;
}
else
flag=1;
}
if(flag) printf("You won't be eaten!\n");
else printf("Uh oh..\n");

}

return 0;
}

sudipta
New poster
Posts: 11
Joined: Wed Sep 30, 2009 7:23 pm
Location: Sylhet

Re: 10945 - Mother Bear

Post by sudipta » Sat Oct 10, 2009 8:56 pm

Help Please. 10945-Mother Bear

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
	char line[100];
	int len,i,j,l,k;
	while(gets(line))
	{
		if(line[0]=='D'&&line[1]=='O'&&line[2]=='N'&&line[3]=='E')break;
		char test[100],x,rev[100];
		j=0;
		int p=0;
		len=strlen(line);
		if(len==0)printf("You won't be eaten!\n");
else{	for(i=0;i<len;i++)
		{
			x=tolower(line[i]);
			if(x<='z' && x>='a')
			{
				test[j]=x;
				j++;
				l=j;
			}
		}
		for(i=0,j=l-1;i<l,j>=0;i++,j--)
		{
				rev[i]=test[j];
		}
		for(k=0;k<l;k++)
		{
			if(test[k]!=rev[k])
			{
				p++;
				break;
			}
		}
		if(p>0) printf("Uh oh..\n");
		else printf("You won't be eaten!\n");
      p=0; }
	}
	return 0;
}
My code is giving the right outputs. But why WA?
Anybody help please.
Don't Copy, Think Also

sudipta
New poster
Posts: 11
Joined: Wed Sep 30, 2009 7:23 pm
Location: Sylhet

10945- MOther Bear

Post by sudipta » Sat Oct 24, 2009 10:21 pm

Please anyone help me. :x
Here is my code. I got WA again and again.

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
	char line[100];
	int len,i,j,l,k;
	while(gets(line))
	{
		len=strlen(line);
		if((len==4)&&(line[0]=='D'&&line[1]=='O'&&line[2]=='N'&&line[3]=='E'))break;
		char test[100],x,rev[100];
		j=0;
		int p=0;
		if(len==0)printf("You won't be eaten!\n");
else{	for(i=0;i<len;i++)
		{
			x=tolower(line[i]);
			if(x<='z' && x>='a')
			{
				test[j]=x;
				j++;
				l=j;
			}
		}
		for(i=0,j=l-1;i<l,j>=0;i++,j--)
		{
				rev[i]=test[j];
		}
		for(k=0;k<l;k++)
		{
			if(test[k]!=rev[k])
			{
				p++;
				break;
			}
		}
		if(p>0) printf("Uh oh..\n");
		else printf("You won't be eaten!\n");
      p=0; }
	}
	return 0;
}
PLeaaaaaaaaaasssssssssssssssseeeeeeeeeeeeeeeeee
Don't Copy, Think Also

sms.islam
New poster
Posts: 19
Joined: Sat Oct 10, 2009 10:28 am

Re: 10945 - Mother Bear

Post by sms.islam » Thu Dec 02, 2010 1:13 am

please help!!! why i am getting WA........
[c]


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define max 50001

char ch[max], str[max] , str1[max] , ch1[max];

void RESULT()
{
int ptr = 0 ;
ptr = strcmp(str1,str);
if(ptr == 0 ) printf("You won't be eaten!");
else printf("Uh oh..");
}

int main(void)
{
while(gets(ch))
{
if(ch[0] =='D' && ch[1] =='O' && ch[2] == 'N' && ch[3] == 'E')
break;
int i , j = 0 , len = 0 , len1 = 0 , l = 0 , k ;
len = strlen(ch);
if(len == 0 ) printf("You won't be eaten!");
else
{
for ( i = 0 ; i < len ; i ++)
{
if((isalpha(ch)) || (ch == ',') || (ch == '.') || (ch == '?') ||(ch =='!'))
{
if(isalpha(ch))
{
if(isupper(ch))
ch = tolower(ch);

ch1[j] = ch;
j ++;
}
}
else
continue;
}
strcpy(str,ch1);
strcpy(str1,ch1);
len1 = strlen(str1);
for ( k = len1-1 ; k >= 0 ; k --)
{
str1[l] = str1[k]; l++;
}
RESULT();
}
printf("\n");
}
return 0;
}

[c]

please tell whats wrong with that code..........

DD
Experienced poster
Posts: 145
Joined: Thu Aug 14, 2003 8:42 am
Location: Mountain View, California
Contact:

Re: 10945 - Mother Bear

Post by DD » Mon Mar 14, 2011 3:04 am

sudipta wrote:Help Please. 10945-Mother Bear

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
	char line[100];
	int len,i,j,l,k;
	while(gets(line))
	{
		if(line[0]=='D'&&line[1]=='O'&&line[2]=='N'&&line[3]=='E')break;
		char test[100],x,rev[100];
		j=0;
		int p=0;
		len=strlen(line);
		if(len==0)printf("You won't be eaten!\n");
else{	for(i=0;i<len;i++)
		{
			x=tolower(line[i]);
			if(x<='z' && x>='a')
			{
				test[j]=x;
				j++;
				l=j;
			}
		}
		for(i=0,j=l-1;i<l,j>=0;i++,j--)
		{
				rev[i]=test[j];
		}
		for(k=0;k<l;k++)
		{
			if(test[k]!=rev[k])
			{
				p++;
				break;
			}
		}
		if(p>0) printf("Uh oh..\n");
		else printf("You won't be eaten!\n");
      p=0; }
	}
	return 0;
}
My code is giving the right outputs. But why WA?
Anybody help please.
You should change your termination condition from:

Code: Select all

if(line[0]=='D'&&line[1]=='O'&&line[2]=='N'&&line[3]=='E')break;
to:

Code: Select all

if(line[0]=='D'&&line[1]=='O'&&line[2]=='N'&&line[3]=='E'&&line[4]==0)break;
Otherwise your program won't perform for input line "DONEENOD", which is a palindrome.

Good luck! :D
Have you ever...
  • Wanted to work at best companies?
  • Struggled with interview problems that could be solved in 15 minutes?
  • Wished you could study real-world problems?
If so, you need to read Elements of Programming Interviews.

Post Reply

Return to “Volume 109 (10900-10999)”