401 - Palindromes

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

Moderator: Board moderators

asif_khan_ak_07
New poster
Posts: 25
Joined: Fri Apr 17, 2009 8:24 am

Re: 401- Palindromes WA. I'm going crazy

Post by asif_khan_ak_07 » Mon Jun 29, 2009 6:44 pm

plz help me to find out the mistake

Code: Select all

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

int main()
{
//freopen("pal.txt","r",stdin);

char lett[]={'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','1','2','3','4','5','6','7','8','9'};
char rev[]={'A','$','$','$','3','$','$','H','I','L','$','J','M','$','O','$','$','$','2','T','U','V','W','X','Y','5','1','S','E','$','Z','$','$','8','$'	};


char a[100];
int l,x,m,ns;

while(gets(a))

{

l=strlen(a);


if(l==1)
m=l;

else if(l%2==0)
m=l/2;

else 
m=(l-1)/2;


ns=0;

for(int j=0;j<m;j++)
{

	for(int k=0;lett[k];k++)
	{
	
		if(a[j]==lett[k])
		{
		
			if(a[l-1-j]==rev[k])
				ns++;
		
		}
	
	
	}



}


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

x=0;

for(int i=0;i<m;i++)
{


	if(a[i]==a[l-1-i])
		x++;
	


}



if(l%2==1)
{

	for(int q=0;lett[q];q++)
	{
	
		if(a[m]==lett[q])
		{
			
			if(a[m]==rev[q])
				ns++;

		}
	
	}


}


//printf("%d %d %d %d ",ns,x,m,l);


if(l%2==1&&ns==m+1&&x==m)
printf("%s -- is a mirrored palindrome.\n",a);

else if(l%2==1&&ns!=m&&x==m)
printf("%s -- is a regular palindrome.\n",a);

else if(l%2==1&&ns==m+1&&x!=m)
printf("%s -- is a mirrored string.\n",a);

else if(l%2==1&&ns==m&&x!=m)
printf("%s -- is not a palindrome.\n",a);

else if(x==m&&ns==m)
printf("%s -- is a mirrored palindrome.\n",a);

else if(ns==m&&x!=m)
printf("%s -- is a mirrored string.\n",a);

else if(x==m&&ns!=m)
printf("%s -- is a regular palindrome.\n",a);
	
else
printf("%s -- is not a palindrome.\n",a);



printf("\n");

}


return 0;
}

User avatar
Jordi Aranda
New poster
Posts: 13
Joined: Wed Apr 29, 2009 11:37 am
Location: Barcelona

Re: 401- Palindromes WA. I'm going crazy

Post by Jordi Aranda » Mon Aug 03, 2009 12:47 am

Be careful with the extra empty line after each output line
Born to be wild

Skt
New poster
Posts: 5
Joined: Wed Oct 29, 2008 2:55 pm

Re: 401- Palindromes

Post by Skt » Tue Sep 22, 2009 4:41 pm

PLZ HELP ME WID THIS AS FAR AS I THINK IM CORRECT


#include<iostream>
using namespace std;
bool ismirr(string);
int main()
{
string str,str1;
bool palin,mirror;
while(cin>>str)
{
str1 = str;
reverse(str.begin(),str.end());
if(str1 == str) palin = 1;
else palin = 0;
mirror = ismirr(str);
if(mirror == 1 && palin == 1) cout<<str1<<" -- is a mirrored palindrome.\n"<<endl;
else
if(mirror == 0 && palin == 0) cout<<str1<<" -- is not a palindrome.\n"<<endl;
else
if(mirror == 0 && palin == 1) cout<<str1<<" -- is a regular palindrome.\n"<<endl;
else
if(mirror == 1 && palin == 0) cout<<str1<<" -- is a mirrored string.\n"<<endl;
}
return 0;
}
bool ismirr(string str)
{
int i,op = 0,l;
string str1,in = "AEHIJLMOSTUVWXYZ12358",fin = "A3HILJMO2TUVWXY51SEZ8";
str1 = str;
l = str.length();
if(l == 1)
{
if(fin[in.find(str[0])] == str[0])
return 1;
else
return 0;
}
else
{
for(i = 0;str1;i++)
{
if(in.find(str))
{
str1 = fin[in.find(str)];
op = 1;
}
}
reverse(str1.begin(),str1.end());
if(str1 == str && op == 1)
return 1;
return 0;
}
}

jfvs
New poster
Posts: 12
Joined: Wed Feb 02, 2011 10:40 am

Re: 401- Palindromes

Post by jfvs » Fri Apr 15, 2011 3:22 am

I dont know whats wrong with my code... I have tried all the test cases in this forum... could you help me out please?

Code: Select all

#include <cstdio>
#include <string.h>

using namespace std;
int main(){
	char mirror[] = {'A',0,0,0,'3',0,0,'H','I','L',0,'J','M',0,'O',0,0,0,'2','T','U','V','W','X','Y','5','1','S','E',0,'Z',0,0,'8',0};
	char word[256];
	while(scanf("%s", &word) != EOF){
		bool mirrored = true,  palindrome = false;
		int i = 0, j = strlen(word) - 1, number = 0;
		for(; i < j; i++, j--)
			if(word[i] != word[j])
				break;
		palindrome = (i >= j)? true: false;
		for(i = 0, j = strlen(word) - 1; i <= j && mirrored; i++, j--){
			int fix = 0;
			fix = (word[i] >= 'A' && word[i] <= 'Z')? 'A' : '1' - 26;
			if(mirror[word[i] - fix] != 0){
				number += (word[i] >= '1' && word[i] <= '9' ||
						   word[j] >= '1' && word[j] <= '9')? 1 : 0;
				mirrored = (mirror[word[i] - fix] == word[j])? mirrored : !mirrored;
			}else{
				mirrored = false;
			}
		}
		if(palindrome && !mirrored)
			printf("%s -- is a regular palindrome.\n\n", word);
		else if(!palindrome && mirrored && number != 0)
			printf("%s -- is a mirrored string.\n\n", word);
		else if(palindrome && mirrored && number != 0)
			printf("%s -- is a mirrored palindrome.\n\n", word);
		else if(!palindrome && !mirrored)
			printf("%s -- is not a palindrome.\n\n", word);
	}
	return 0;
}

lprell
New poster
Posts: 5
Joined: Fri Jun 17, 2011 8:56 am

Re: 401 palindromes WA help

Post by lprell » Tue Aug 09, 2011 11:06 pm

Need some help.

I keep getting WA. Checked with all output in threads and it seems ok. Extra lines also ok.

Heres my code.
Thanks for the help and sorry my bad coding. Still learning...

Code: Select all

# include <stdio.h>
# include <stdlib.h>

void inverte (char *string)
{
   int i,j;
   char h;
   for (i=0, j=strlen(string)-1; i<j; ++i, --j)
   {
      h= string[i];
      string[i]= string[j];
      string[j]= h;
   }
}

int reverso (char *string)
{
   char r[13]="AHIMOTUVWXY18";
   int i,j,aux=0;
   for (i=0; i<strlen(string); ++i)
   {
      for (j=0; j<13; ++j)
      {
         if (string[i]==r[j])
            aux++;
      }
   }
   return aux;
}

void substitui (char *string)
{
   int i,aux;
   for (i=0; i<strlen(string); ++i)
   {
      if (string[i] == 'E')
      {
         string[i]= '3';
         aux++;
      }
      else
         if (string[i] == 'J')
         {
            string[i]= 'L';
            aux++;
         }
         else
            if (string[i] == 'L')
            {
               string[i]= 'J';
               aux++;
            }
            else
               if (string[i] == 'Z')
               {
                  string[i]= '5';
                  aux++;
               }
               else
                  if (string[i] == '2')
                  {
                     string[i]= 'S';
                     aux++;
                  }
                  else
                     if (string[i] == '3')
                     {
                        string[i]= 'E';
                        aux++;
                     }
                     else
                        if (string[i] == '5')
                        {
                           string[i]= 'Z';
                           aux++;
                        }
                        else
                           if (string[i] == 'S')
                           {
                              string[i]= '2';
                              aux++;
                           }
   }
}

int main (void)
{
   char s[50],t[50];
   int i,cont,aux;
   while (gets(s) != EOF)
   {
      cont=-1;
      strcpy(t,s);
      inverte(t);      
      if (strcmp(s,t)==0)
      {
         cont=1;
      }
      substitui(t);
      if (strcmp(s,t)==0)
      {
         cont--;
      }
      aux=reverso(t);
      if (cont==-1)
      {
         for (i=0; i<strlen(t); ++i)
         {
            printf("%c", s[i]);
         }
         printf(" -- is not a palindrome.\n\n");
      }else
         if (cont==1)
         {
            for (i=0; i<strlen(s); ++i)
            {
               printf("%c", s[i]);
            }
            printf(" -- is a regular palindrome.\n\n");
         }else
            if (cont==-2)
            {
               for (i=0; i<strlen(s); ++i)
               {
                  printf("%c", s[i]);
               }
               printf(" -- is a mirrored string.\n\n");
            }else
               if (cont==0)
               {
                  for (i=0; i<strlen(s); ++i)
                  {
                     printf("%c", s[i]);
                  }
                  if (aux==strlen(s))
                     printf(" -- is a mirrored palindrome.\n\n");
                  else
                     printf(" -- is a regular palindrome.\n\n");
               }
   }
   return 0;
}


lprell
New poster
Posts: 5
Joined: Fri Jun 17, 2011 8:56 am

Re: 401- Palindromes

Post by lprell » Wed Aug 10, 2011 11:25 pm

Why i keep getting Runtime Error??? Im going crazy whith this...
You guys have a clue? Please help me.
Here is my code.

Code: Select all

REMOVED AFTER AC!
Last edited by lprell on Sat Sep 10, 2011 10:48 pm, edited 1 time in total.

Val
New poster
Posts: 1
Joined: Mon Jun 28, 2010 12:12 pm

Re: 401- Palindromes

Post by Val » Thu Aug 25, 2011 4:11 pm

lprell wrote:Why i keep getting Runtime Error??? Im going crazy whith this...
You guys have a clue? Please help me.
Here is my code.

Code: Select all

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

int main()
{
   char rev[]="JSEZL235AHIMOTUVWXY18";
   char input[20], temp[20], c;
   int i,j, cont, pos, aux;
   while (scanf("%s", &input)!= EOF)
   {
      aux=0;
      cont=-1;
      strcpy(temp,input);
      i=0;
      for (i=0, j=strlen(temp)-1; i<j; ++i, --j)
      {
         c= temp[i];
         temp[i]= temp[j];
         temp[j]= c;
      }
      if (strcmp(input,temp)==0)
      {
         cont=1;
      }
      for (i=0; i<strlen(temp); ++i)
      {
         for (j=0; j<21; ++j)
         {
            if (temp[i]==rev[j])
            {
               aux++;
               pos=j;
               if (pos>7)
               {
                  temp[i]=rev[j];
                  break;
               }else
                  if(pos<4)
                  {
                     temp[i]=rev[j+4];
                     break;
                  }else
                     {
                        temp[i]=rev[j-4];
                        break;
                     }
            }
         }
      }
      if (strcmp(input,temp)==0 && aux==strlen(input))
      {
         cont--;
      }
      switch(cont)
      {
         case 1:
            printf("%s", input);
            printf(" -- is a regular palindrome.\n\n");
            break;
         case -2:
            printf("%s", input);
            printf(" -- is a mirrored string.\n\n");
            break;
         case 0:
            printf("%s", input);
            printf(" -- is a mirrored palindrome.\n\n");
            break;
         default: 
            printf("%s", input);
            printf(" -- is not a palindrome.\n\n");
            break;
      }
   }
   return 0;
}
I think your char array "input" (and "temp") is not big enough.
Because:
Input consists of strings (one per line) each of which will consist of one to twenty valid characters.

lprell
New poster
Posts: 5
Joined: Fri Jun 17, 2011 8:56 am

Re: 401- Palindromes

Post by lprell » Sat Sep 10, 2011 10:47 pm

[/code][/quote]

I think your char array "input" (and "temp") is not big enough.
Because:
Input consists of strings (one per line) each of which will consist of one to twenty valid characters.
[/quote]

Many thanks Val !! Got AC after changing the size of array!

i see
New poster
Posts: 9
Joined: Sat May 19, 2012 7:35 am

401_Please help me and tell me where is the wrong?

Post by i see » Fri Jun 01, 2012 4:02 pm

[*]I have tried again and again,but i can't get ac all the time!All though it's easy,please help me ! thanks in advance.

Code: Select all

#include <iostream>
#include <fstream>
#include <string>
//#define LOCAL
#define fin cin
#define fout cout
using namespace std;

bool is_palindrome_string(const string &s)
{
     for(int i=0;i<=s.length()-i-1;++i)
         if(s[i]!=s[s.length()-i-1])  return false;
     return true;   
     }
     
bool is_mirrored_string(const string &s)
{
     
     bool log;
     string ss[23]={"E3","JL","LJ","S2","Z5","2S","3E","5Z","AA","HH","II","OO","TT","UU","VV","XX","WW","YY","11","88","MM"};
     for(int i=s.length()-1;i>=0;--i)
         {
          log=false;
          for(int j=0;j<21;++j)
             if(ss[j][0]==s[i])
               {log=true;
                if(s[s.length()-i-1]!=ss[j][1]) return false ;
                } 
          if(!log) return false;
          }
     return true;
     }

int main()
{
    #ifdef LOCAL
    ifstream fin("in.cpp");
    ofstream fout("out.cpp");
    #endif
    string s;
    while(fin>>s)
    {
      if(is_palindrome_string(s)) {
             if(is_mirrored_string(s))  fout<< s<<" -- is a mirrored palindrome."<<endl; 
             else fout<<s<<" -- is a regular palindrome." <<endl;
             }
      else  {
            if(is_mirrored_string(s)) fout<<s<<" -- is a mirrored string." <<endl;
            else fout<<s<<" -- is not a palindrome." <<endl;
      }
      fout<<endl;    
    }
    
    return 0;
}


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

Re: 401_Please help me and tell me where is the wrong?

Post by brianfry713 » Fri Jun 01, 2012 11:38 pm

Your code doesn't work for strings of length 1.

s.length() returns a size_t unsigned integer. So taking s.length()-i-1 could give a negative result, causing the limit of your for loop i<=s.length()-i-1 to not give the results you expected.
Check input and AC output for thousands of problems on uDebug!

i see
New poster
Posts: 9
Joined: Sat May 19, 2012 7:35 am

Re: 401_Please help me and tell me where is the wrong?

Post by i see » Sat Jun 02, 2012 9:06 am

Hi, brianfry713 , I want to thank you for making me understand the error where i did't find all the time , i have passed the problem,thank you again !

User avatar
mahade hasan
Learning poster
Posts: 87
Joined: Thu Dec 15, 2011 3:08 pm
Location: University of Rajshahi,Bangladesh

Re: 401_Please help me and tell me where is the wrong?

Post by mahade hasan » Sun Jul 01, 2012 10:28 am

cut>>>>After aCC
Last edited by mahade hasan on Mon Aug 06, 2012 12:46 am, edited 1 time in total.
we r surrounded by happiness
need eyes to feel it!

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

Re: 401_Please help me and tell me where is the wrong?

Post by brianfry713 » Mon Jul 02, 2012 9:45 pm

AE -- is not a palindrome.
Check input and AC output for thousands of problems on uDebug!

sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

Re: 401 palindromes WA help

Post by sith » Wed Jul 04, 2012 11:21 pm

Hello

I believe that my solution is correct but I've got WA.
Maybe the bad output formatting is the reason of WA?

Code: Select all

AC


wangluofan
New poster
Posts: 6
Joined: Tue Aug 07, 2012 7:00 pm

401-Palindromes

Post by wangluofan » Tue Aug 07, 2012 7:08 pm

Code: Select all

#include<stdio.h>
#include<string.h>
#define N 30
char reverse[]={"A000300HIL0JM0O0002TUVWXY51SE0Z0080"};

bool isRegular(char* str)
{
	bool flag=true;
	int beg=0,end=strlen(str)-1;
	while(beg<=end)
	{
		if(str[beg]!=str[end])
		{
			flag=false;
			break;
		}
		++beg;
		--end;
	}
	return flag;
}

bool isMirrored(char* str)
{
	bool flag=true;
	char rev[N];
	int cnt=0;
	for(int i=0;i!=strlen(str);++i)
	{
		if('0'<=str[i]&&'9'>=str[i])
			rev[cnt++]=reverse[str[i]-'1'+26];
		else
			rev[cnt++]=reverse[str[i]-'A'];
	}
	rev[cnt]='\0';
	int len=strlen(str)-1;
	for(int beg=0,end=len;beg<=len&&end>=0;++beg,--end)
	{
		if(str[beg]!=rev[end])
		{
			flag=false;
			break;
		}
	}
	return flag;
}

int main(void)
{
	char str[N];
	while(fscanf(stdin,"%s",str)!=EOF)
	{
		if(isRegular(str)&&!isMirrored(str))
			printf("%s -- is a regular palindrome.\n",str);
		else if(!isRegular(str)&&isMirrored(str))
			printf("%s -- is a mirrored string.\n",str);
		else if(isRegular(str)&&isMirrored(str))
			printf("%s -- is a mirrored palindrome.\n",str);
		else
			printf("%s -- is not a palindrome.\n",str);
	}
	return 0;
}
Please Tell me where is the wrong,Thank you very much!!

Post Reply

Return to “Volume 4 (400-499)”