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

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

Re: 401-Palindromes

Post by brianfry713 »

after each output line, you must print an empty line.
Check input and AC output for thousands of problems on uDebug!
wangluofan
New poster
Posts: 6
Joined: Tue Aug 07, 2012 7:00 pm

Re: 401-Palindromes

Post by wangluofan »

brianfry713 wrote:after each output line, you must print an empty line.
You're right,The program is AC,Thank you very much.
sumit saha shawon
New poster
Posts: 19
Joined: Tue Jun 26, 2012 9:19 pm

Re: 401-Palindromes

Post by sumit saha shawon »

What is problem in my code???
My input output is ok.
code:
#include<stdio.h>
#include<string.h>
char str[50],ar[50],temp[50];
int main()
{
while(gets(str))
{
printf("%s",str);
int palindrome=0,mirror_strig=0;
int len=strlen(str),i,j=0;


for(i=0;i<len;i++)
temp=str;
temp='\0';
// puts(temp);


for(i=len-1; i>=0; i--)
ar[j++]=str;
ar[j]='\0';
// puts(ar);
if(strcmp(ar,str)==0)
palindrome=1;
// printf("%d\n",palindrome);


for(i=0; i<len; i++)
{
if(str=='E')
str='3';
else if(str=='J')
str='L';
else if(str=='L')
str='J';
else if(str[i]=='S')
str[i]='2';
else if(str[i]=='Z')
str[i]='5';
else if(str[i]=='2')
str[i]='S';
else if(str[i]=='3')
str[i]='E';
else if(str[i]=='5')
str[i]='Z';

}



//puts(str);
j=0;
for(i=len-1; i>=0; i--)
ar[j++]=str[i];
ar[j]='\0';
//puts(ar);


if(strcmp(ar,temp)==0)
mirror_strig=1;



//printf("%d\n",mirror_strig);
if(palindrome==0&&mirror_strig==0)
puts(" -- is not a palindrome.");
else if(palindrome==1&&mirror_strig==0)
puts(" -- is a regular palindrome.");
else if(palindrome==0&&mirror_strig==1)
puts(" -- is a mirrored string.");
else if(palindrome==1&&mirror_strig==1)
puts(" -- is a mirrored palindrome.");
puts("");


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

Re: 401-Palindromes

Post by brianfry713 »

Your code doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!
kier.guevara
New poster
Posts: 30
Joined: Thu Jul 19, 2012 11:24 pm

Re: 401- Palindromes

Post by kier.guevara »

I tried all the test cases here but I still got WA..
What's wrong with it?

Code: Select all

#include <iostream>

using namespace std;

bool mirrored(string palindrome)
{
	int counter = 0;
	
	char mirr[20][2] = {{'A','A'},{'E','3'},{'H','H'},{'I','I'}
		,{'J','L'},{'L','J'},{'M','M'} ,{'O','O'},{'T','T'},{'U','U'}
		,{'V','V'},{'W','W'},{'X','X'},{'Y','Y'},{'Z','5'},{'1','1'}
		,{'2','S'},{'3','E'},{'5','Z'},{'8','8'}};
		
	
		
			if(palindrome.length() == 1)
	{
		
		for(int j = 0; j < 20; j++)
		{
			if(palindrome[0] == mirr[j][0])
				if(palindrome[0] == mirr[j][1])
				{
					counter++;
					break;
				}
		}
		if(counter == 1)
		{
			return true;
		}
		else if(counter == 0)
			 return false;
   }
		
	if(palindrome.length() % 2 == 0)
	{
	for(int i = 0; i < palindrome.length()/2; i++)
	{
		for(int j = 0; j < 20; j++)
		{
			if(palindrome[i] == mirr[j][0])
				if(palindrome[(palindrome.length()-1) - i] == mirr[j][1])
				{
					counter++;
					break;
				}
		}
		
	
	}
		
			if(counter == palindrome.length()/2)
				return true;
			else 
				return false;
		
	}
	
		if(palindrome.length() % 2 == 1)
		{
			for(int i = 0; i < palindrome.length()/2 + 1; i++)
	{
		for(int j = 0; j < 20; j++)
		{
			if(palindrome[i] == mirr[j][0])
				if(palindrome[(palindrome.length()-1) - i] == mirr[j][1])
				{
					counter++;
					break;
				}
		}
		
	
	}
			if(counter == palindrome.length()/2 + 1)
			return true;
			else
				return false;
		}

	

			 
	
	
	return false;
}
bool regPalindrome(string palindrome)
{
	int counter = 0;
	for(int i = 0; i < palindrome.length()/2; i++)
	{
		if(palindrome[i] == palindrome[(palindrome.length()-1) - i])
			counter++;
	}
	
	if(counter == palindrome.length()/2)
		return true;
		
	return false;
}


int main()
{
	string palindrome;
	
	while(cin >> palindrome)
	{
		
		
	    if(regPalindrome(palindrome) == true && mirrored(palindrome) == true)
			cout << palindrome << " -- is a mirrored palindrome." << endl;
		else if(mirrored(palindrome) == true)
			cout << palindrome << " -- is a mirrored string." << endl;
		else if(regPalindrome(palindrome) == true)
			cout << palindrome << " -- is a regular palindrome." << endl;
		else
			cout << palindrome << " -- is not a palindrome." << endl;
			
		cout << endl;
	}
	return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 401- Palindromes

Post by brianfry713 »

S2 -- is a mirrored string.
Check input and AC output for thousands of problems on uDebug!
kullalok'
New poster
Posts: 1
Joined: Sat Jun 08, 2013 12:38 am

401: Palindromes Wrong Answer

Post by kullalok' »

I don't see why I am getting a wrong answer here. Any suggestions will be appreciated

Code: Select all

import java.io.*;
import java.util.*;

public class UVaPalindromes {
    
    static TreeMap<Character, Character> reverses;
    
    public static void main( String[] args ) throws IOException {
        
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        PrintWriter out = new PrintWriter( System.out, true );
        
        reverses = new TreeMap<Character, Character>();
    
        reverses.put( 'A', 'A' );
        reverses.put( 'E', '3' );
        reverses.put( '3', 'E' );
        reverses.put( 'H', 'H' );
        reverses.put( 'I', 'I' );
        reverses.put( 'J', 'L' );
        reverses.put( 'L', 'J' );
        reverses.put( 'M', 'M' );
        reverses.put( 'O', 'O' );
        reverses.put( 'T', 'T' );
        reverses.put( 'U', 'U' );
        reverses.put( 'V', 'V' );
        reverses.put( 'W', 'W' );
        reverses.put( 'X', 'X' );
        reverses.put( 'Y', 'Y' );
        reverses.put( 'Z', '5' );
        reverses.put( '1', '1' );
        reverses.put( '2', 'S' );
        reverses.put( '5', 'Z' );
        reverses.put( '8', '8' );
        
        while( true ) {
            
            String s = in.readLine();
            
            if( s == null ) {
                break;
            }
            
            boolean palindrome = palindrome( s );
            boolean mirror = mirror( s );
            
            if( palindrome && mirror ) {
                out.println( s + " -- is a mirrored palindrome." );
            }
            else if( palindrome ) {
                out.println( s + " -- is a regular palindrome." );
            }
            else if( mirror ) {
                out.println( s + " -- is a mirrored string." );
            }
            else {
                out.println( s + " -- is not a palindrome." );
            }
            
            out.println();
        }
    }
    
    public static boolean palindrome( String s ) {
        
        for( int i = 0; i < s.length() / 2; i++ ) {
            
            if( s.charAt( i ) != s.charAt( s.length() - i - 1 ) ) {
                return false;
            }
        }
        
        return true;
    }
    
    public static boolean mirror( String s ) {
        
        for( int i = s.length() - 1; i >= 0; i-- ){
   
            if( !reverses.containsKey( s.charAt( i ) ) || reverses.get( s.charAt( i ) ) != s.charAt( s.length() - i - 1 ) )
                return false;
        }
   
        return true;

    }
}
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: 401: Palindromes Wrong Answer

Post by sohel »

http://acm.uva.es/board/viewtopic.php?f=5&t=5863

Search the board first using the 'search' option located at the top right. Don't create a new thread for a problem that already exists. Make your post in an existing thread.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 401: Palindromes Wrong Answer

Post by brianfry713 »

use class Main
Check input and AC output for thousands of problems on uDebug!
Wishu.here
New poster
Posts: 3
Joined: Sat Jun 15, 2013 9:49 pm

401 Palindromes WA help!!!

Post by Wishu.here »

Could not find why this code showing wrong answer?? i have tested all the available output of this forum like below:

NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
MAIAM
123ESI
123ES1
DEO3D
9339
A
B
E
M
1
3
2
4

but still getting wrong answer from online judge :(

Code: Select all


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

int palindrome_check();
int mirror_check();

int main()
{
    char input[1000];
    int p,q,i;

    while(scanf("%s",&input)!=EOF)
    {
        for(i=0;i<strlen(input);i++)
            {
                if(input[i]=='0')
                    {
                        input[i]='O';
                    }
            }

        p=palindrome_check(input);
        q=mirror_check(input);

        if(p==0&&q==0)
            {
                printf("%s -- is not a palindrome.",input);
            }
        else if(p==1&&q==0)
            {
                printf("%s -- is a regular palindrome.",input);
            }
        else if(p==0&&q==1)
            {
                printf("%s -- is a mirrored string.",input);
            }
        else if(p==1&&q==1)
            {
                printf("%s -- is a mirrored palindrome.",input);
            }
        printf("\n\n");
    }
    return 0;
}

int palindrome_check(char input[1000])
{
    char reversed_string[1000];
     int string_length,i,flag=0,j=0;

     for(i=strlen(input)-1;i>=0;i--)
        {
            reversed_string[i]=input[j];
            j++;
        }



     string_length=strlen(input);

     for(i=0;i<string_length;i++)
        {
            if(input[i]==reversed_string[i])
            {

            }
            else
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            {
                return 0;
            }
        else
            {
                return 1;
            }

}


int mirror_check(char input[1000])
{
    int i=0,j=0,k=0,flag=0,m;
    char l[25][25],str1[1000],str2[1000];


    l[0][0]='A';
    l[1][0]='E';
    l[2][0]='H';
    l[3][0]='I';
    l[4][0]='J';
    l[5][0]='L';
    l[6][0]='M';
    l[7][0]='O';
    l[8][0]='S';
    l[9][0]='T';
    l[10][0]='U';
    l[11][0]='V';
    l[12][0]='W';
    l[13][0]='X';
    l[14][0]='Y';
    l[15][0]='Z';
    l[16][0]='1';
    l[17][0]='2';
    l[18][0]='3';
    l[19][0]='5';
    l[20][0]='8';

    l[0][1]= 'A' ;
    l[1][1]='3' ;
    l[2][1]='H' ;
    l[3][1]='I' ;
    l[4][1]='L' ;
    l[5][1]='J' ;
    l[6][1]='M' ;
    l[7][1]='O' ;
    l[8][1]='2' ;
    l[9][1]='T' ;
    l[10][1]='U' ;
    l[11][1]='V' ;
    l[12][1]='W' ;
    l[13][1]='X' ;
    l[14][1]='Y' ;
    l[15][1]='5' ;
    l[16][1]='1' ;
    l[17][1]='S' ;
    l[18][1]='E' ;
    l[19][1]='Z' ;
    l[20][1]='8' ;

    strcpy(str1,input);

    for(i=strlen(input)-1;i>=0;i--)
        {
            str2[i]=input[j];
            j++;
        }
        j=0;




    for(k=0;k<strlen(str1);k++)
    {

        for(i=0;i<21;i++)
            {
                for(j=0;j<2;j++)
                    {
                        if(str1[k]==l[i][j])
                            {
                                if(j==0)
                                    {
                                        m=j;
                                        m++;
                                        if(str2[k]==l[i][m])
                                        {
                                            flag=1;
                                            break;
                                        }
                                        else
                                            flag=0;
                                            break;
                                    }
                                else if(j==1)
                                    {
                                        m=j;
                                        m--;
                                        if(str2[k]==l[i][m])
                                        {
                                            flag=1;
                                            break;
                                        }
                                        else
                                            flag=0;
                                            break;
                                    }

                              }
                        }
                }
                if(flag==0)
                {
                    return 0;
                }
    }

return 1;

}


Wishu.here
New poster
Posts: 3
Joined: Sat Jun 15, 2013 9:49 pm

401 Palindromes WA help!!!

Post by Wishu.here »

Could not find why this code showing wrong answer?? i have tested all the available output of this forum like below:

NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
MAIAM
123ESI
123ES1
DEO3D
9339
A
B
E
M
1
3
2
4

but still getting wrong answer from online judge :(

Code: Select all


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

int palindrome_check();
int mirror_check();

int main()
{
    char input[1000];
    int p,q,i;

    while(scanf("%s",&input)!=EOF)
    {
        for(i=0;i<strlen(input);i++)
            {
                if(input[i]=='0')
                    {
                        input[i]='O';
                    }
            }

        p=palindrome_check(input);
        q=mirror_check(input);

        if(p==0&&q==0)
            {
                printf("%s -- is not a palindrome.",input);
            }
        else if(p==1&&q==0)
            {
                printf("%s -- is a regular palindrome.",input);
            }
        else if(p==0&&q==1)
            {
                printf("%s -- is a mirrored string.",input);
            }
        else if(p==1&&q==1)
            {
                printf("%s -- is a mirrored palindrome.",input);
            }
        printf("\n\n");
    }
    return 0;
}

int palindrome_check(char input[1000])
{
    char reversed_string[1000];
     int string_length,i,flag=0,j=0;

     for(i=strlen(input)-1;i>=0;i--)
        {
            reversed_string[i]=input[j];
            j++;
        }



     string_length=strlen(input);

     for(i=0;i<string_length;i++)
        {
            if(input[i]==reversed_string[i])
            {

            }
            else
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            {
                return 0;
            }
        else
            {
                return 1;
            }

}


int mirror_check(char input[1000])
{
    int i=0,j=0,k=0,flag=0,m;
    char l[25][25],str1[1000],str2[1000];


    l[0][0]='A';
    l[1][0]='E';
    l[2][0]='H';
    l[3][0]='I';
    l[4][0]='J';
    l[5][0]='L';
    l[6][0]='M';
    l[7][0]='O';
    l[8][0]='S';
    l[9][0]='T';
    l[10][0]='U';
    l[11][0]='V';
    l[12][0]='W';
    l[13][0]='X';
    l[14][0]='Y';
    l[15][0]='Z';
    l[16][0]='1';
    l[17][0]='2';
    l[18][0]='3';
    l[19][0]='5';
    l[20][0]='8';

    l[0][1]= 'A' ;
    l[1][1]='3' ;
    l[2][1]='H' ;
    l[3][1]='I' ;
    l[4][1]='L' ;
    l[5][1]='J' ;
    l[6][1]='M' ;
    l[7][1]='O' ;
    l[8][1]='2' ;
    l[9][1]='T' ;
    l[10][1]='U' ;
    l[11][1]='V' ;
    l[12][1]='W' ;
    l[13][1]='X' ;
    l[14][1]='Y' ;
    l[15][1]='5' ;
    l[16][1]='1' ;
    l[17][1]='S' ;
    l[18][1]='E' ;
    l[19][1]='Z' ;
    l[20][1]='8' ;

    strcpy(str1,input);

    for(i=strlen(input)-1;i>=0;i--)
        {
            str2[i]=input[j];
            j++;
        }
        j=0;




    for(k=0;k<strlen(str1);k++)
    {

        for(i=0;i<21;i++)
            {
                for(j=0;j<2;j++)
                    {
                        if(str1[k]==l[i][j])
                            {
                                if(j==0)
                                    {
                                        m=j;
                                        m++;
                                        if(str2[k]==l[i][m])
                                        {
                                            flag=1;
                                            break;
                                        }
                                        else
                                            flag=0;
                                            break;
                                    }
                                else if(j==1)
                                    {
                                        m=j;
                                        m--;
                                        if(str2[k]==l[i][m])
                                        {
                                            flag=1;
                                            break;
                                        }
                                        else
                                            flag=0;
                                            break;
                                    }

                              }
                        }
                }
                if(flag==0)
                {
                    return 0;
                }
    }

return 1;

}


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

Re: 401 Palindromes WA help!!!

Post by brianfry713 »

don't double post
Check input and AC output for thousands of problems on uDebug!
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 401 Palindromes WA help!!!

Post by brianfry713 »

0 -- is a regular palindrome.

O -- is a mirrored palindrome.
Check input and AC output for thousands of problems on uDebug!
cosmin79
New poster
Posts: 11
Joined: Fri Aug 09, 2013 7:25 pm

Re: 401 Palindromes WA help!!!

Post by cosmin79 »

Code: Select all

#include <cstdio>
#include <iostream>
#include <string>
#define NMAX 255
using namespace std;
char rev[NMAX];
int n;
string A, B;

void prep()
{
    rev['E'] = '3'; rev['J'] = 'L'; rev['L'] = 'J';
    rev['S'] = '2'; rev['Z'] = '5'; rev['2'] = 'S'; 
    rev['3'] = 'E'; rev['5'] = 'Z';
}

int main()
{
    //freopen("input", "r", stdin);
    prep();
    int i, type, t = 0;
    while (cin >> A)
    {
        n = A.size(); type = 0; t++;
        if (t > 1)
            printf("\n");
            
        type ^= 1;
        for (i = 1; i <= n / 2; i++)
            if (A[i - 1] != A[n - i])
            {
                type ^= 1;
                break ;
            }
        
        B = A;
        for (i = 0; i < n; i++)
            if (rev[B[i]])
                B[i] = rev[B[i]];
        
        type ^= 2;
        for (i = 1; i <= n; i++)
            if (A[i - 1] != B[n - i])
            {
                type ^= 2;
                break ;
            }
        
        cout << A;
        switch (type)
        {
            case 0: cout << " -- is not a palindrome.\n";
                    break ;
            case 1: cout << " -- is a regular palindrome.\n";
                    break ;
            case 2: cout << " -- is a mirrored string.\n";
                    break ;
            case 3: cout << " -- is a mirrored palindrome.\n";
                    break ;
        }
    }
    return 0;
}

Could anyone give me some troublesome tests? My approach is pretty straight forward ; I don't see what can possibly go wrong. I don't quite understand the sentence in the statement regarding 'O' and '0'. It does say that only 'O' is a valid character. However, it also does say that all the characters in the given strings are valid (therefore, there isn't any chance of meeting the character '0', right?).
Thanks in advance!
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 401 Palindromes WA help!!!

Post by brianfry713 »

In addition, after each output line (including the last one), you must print an empty line.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 4 (400-499)”