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

Post Reply
C8H10N4O2
Experienced poster
Posts: 137
Joined: Wed Feb 27, 2002 2:00 am
Location: Pasadena, CA

401 - Palindromes

Post by C8H10N4O2 »

I am getting WA. This is my first attempt at JAVA. Can someone tell me how to fix it and get AC? I am trying to learn JAVA. I suspect it is an input problem.

[java]
import java.io.*;
import java.util.*;

class Main
{
static boolean isPalindrome(String Phrase)
{
int i;
for(i=0;i<Phrase.length();i++)
{
if(Phrase.charAt(i)!=Phrase.charAt(Phrase.length()-i-1))
return false;
}
return true;
}

static boolean isMirrored(String Phrase)
{
int i,Location;
String Original="AEHIJLMOSTUVWXYZ12358";
String Mirrored="A3HILJMO2TUVWXY51SEZ8";

for(i=0;i<Phrase.length();i++)
{
Location=Original.indexOf(Phrase.charAt(i));
if(Location==-1)
return false;
else
if(Mirrored.charAt(Location)!=Phrase.charAt(Phrase.length()-i-1))
return false;
}
return true;
}

public static void main(String args[])
{
byte Buffer[] = new byte[1000];
int Length;
boolean Palindrome,Mirrored;

while(true)
{
try
{
Length=System.in.read(Buffer);
if(Length<=1)
break;
System.out.print(new String(Buffer,0,Length-1));
Palindrome=isPalindrome(new String(Buffer,0,Length-1));
Mirrored=isMirrored(new String(Buffer,0,Length-1));
System.out.print(" -- ");
if(Palindrome)
if(Mirrored)
System.out.println("is a mirrored palindrome.");
else
System.out.println("is a regular palindrome.");
else
if(Mirrored)
System.out.println("is a mirrored string.");
else
System.out.println("is not a palindrome.");
System.out.println("");
}
catch (IOException e)
{
break;
}
}
}
};
[/java]
Melon Melon
New poster
Posts: 17
Joined: Fri May 31, 2002 6:30 pm
Contact:

Help for 401

Post by Melon Melon »

I get wrong answer for this question(401).
Please help me to see where get wrong!
Ths a lot!!
[c]
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int palindrome(char str[])
{
int length, i;
length = strlen(str);

for(i = 0; i < length/2; i++)
{
if(str != str[length - 1 - i])
return 0;
}

return 1;
}

int mirror(char str[])
{
char rev[500];
int i = 0;
int len = strlen(str);
len--;

rev['A'] = 'A';
rev['B'] = '*';
rev['C'] = '*';
rev['D'] = '*';
rev['E'] = '3';
rev['F'] = '8';
rev['G'] = '*';
rev['H'] = 'H';
rev['I'] = 'I';
rev['J'] = 'L';
rev['K'] = '*';
rev['M'] = 'M';
rev['N'] = '*';
rev['O'] = 'O';
rev['P'] = '*';
rev['Q'] = '*';
rev['R'] = '*';
rev['S'] = '2';
rev['T'] = 'T';
rev['U'] = 'U';
rev['V'] = 'U';
rev['W'] = 'W';
rev['X'] = 'X';
rev['Y'] = 'Y';
rev['Z'] = '5';
rev['1'] = '1';
rev['2'] = 'S';
rev['3'] = 'E';
rev['4'] = '*';
rev['5'] = 'Z';
rev['6'] = '*';
rev['7'] = '*';
rev['8'] = '8';
rev['9'] = '*';

while(i <= len)
{
if(rev[(int)str] == '*')
return 0;
else if(rev[(int)str[i++]] != str[len--])
return 0;
}

return 1;
}

int main(int argv, char *argc[])
{
char str[1000000];
int pal, mir;

while(scanf("%s", str) != EOF)
{
pal = palindrome(str);
mir = mirror(str);

if(pal && mir)
printf("%s -- is a mirrored palindrome.\n\n", str);
else if(pal)
printf("%s -- is a regular palindrome.\n\n", str);
else if(mir)
printf("%s -- is a mirrored string.\n\n", str);
else
printf("%s -- is not a palindrome.\n\n", str);
}
return 0;
}

[/c]
xenon
Learning poster
Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland

Post by xenon »

[c] rev['F'] = '8';[/c]
The shift-key gets sticky if you use it often...
Melon Melon
New poster
Posts: 17
Joined: Fri May 31, 2002 6:30 pm
Contact:

Post by Melon Melon »

Sorry...
i dont understand what u mean....
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN »

I think Xeron means the line should be *shift-8*, but not *8* alone. i.e.


[c] rev['F'] = '*'; [/c]
Melon Melon
New poster
Posts: 17
Joined: Fri May 31, 2002 6:30 pm
Contact:

Post by Melon Melon »

i got accepted..
Ths a lot!!^^
udcp
New poster
Posts: 6
Joined: Fri Jun 21, 2002 10:24 pm
Location: USA

401 : Can anyone help me??

Post by udcp »

I submitted the code shown below, and received a WA :( . Can anyone help me in finding out where I went wrong???

/*"@BEGIN_OF_SOURCE_CODE"*/
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <ctype.h>

int isPalindrome(char input[])
{
int strlength;
strlength = strlen(input);

for(int i = 0; i < strlength/2; i++)
{
if(input != input[strlength - i - 1])
return 0;
}

return 1;
}

int isMirror(char input[])
{
char validchars[] = "AEHIJLMOSTUVWXYZ12358";
char mirrorchars[] = "A3HILJMO2TUVWXY51SEZ8";

int strlength = strlen(input);

for (int i = 0; i < strlength; i++)
{
char* pdest = strchr(validchars,input);
int result = pdest - validchars + 1;

if (pdest == NULL)
return 0;
else
if (mirrorchars[result-1] != input[strlength - i - 1])
return 0;
}
return 1;
}

int main(int argv, char *argc[])
{
char input[100000];
int palindrome, mirror;

while (cin >> input)
{
palindrome = isPalindrome(input);
mirror = isMirror(input);

if(palindrome && mirror)
cout <<input<<" -- is a mirrored palindrome.\n\n";
else if(palindrome)
cout <<input<<" -- is a regular palindrome.\n\n";
else if(mirror)
cout <<input<<" -- is a mirrored string.\n\n";
else
cout <<input<<" -- is not a palindrome.\n\n";

}

return 0;
}
/*"@END_OF_SOURCE_CODE"*/
udcp
New poster
Posts: 6
Joined: Fri Jun 21, 2002 10:24 pm
Location: USA

Help me correct the error??

Post by udcp »

I submitted the code shown below, and received a WA . Can anyone help me in finding out where I went wrong???

[cpp]
/*"@BEGIN_OF_SOURCE_CODE"*/
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <ctype.h>

int isPalindrome(char input[])
{
int strlength;
strlength = strlen(input);

for(int i = 0; i < strlength/2; i++)
{
if(input != input[strlength - i - 1])
return 0;
}

return 1;
}

int isMirror(char input[])
{
char validchars[] = "AEHIJLMOSTUVWXYZ12358";
char mirrorchars[] = "A3HILJMO2TUVWXY51SEZ8";

int strlength = strlen(input);

for (int i = 0; i < strlength; i++)
{
char* pdest = strchr(validchars,input);
int result = pdest - validchars + 1;

if (pdest == NULL)
return 0;
else
if (mirrorchars[result-1] != input[strlength - i - 1])
return 0;
}
return 1;
}

int main(int argv, char *argc[])
{
char input[100000];
int palindrome, mirror;

while (cin >> input)
{
palindrome = isPalindrome(input);
mirror = isMirror(input);

if(palindrome && mirror)
cout <<input<<" -- is a mirrored palindrome.\n\n";
else if(palindrome)
cout <<input<<" -- is a regular palindrome.\n\n";
else if(mirror)
cout <<input<<" -- is a mirrored string.\n\n";
else
cout <<input<<" -- is not a palindrome.\n\n";

}

return 0;
}
/*"@END_OF_SOURCE_CODE"*/
[/cpp]
mido
Learning poster
Posts: 78
Joined: Sun Jun 16, 2002 9:48 pm
Location: Cairo,Egypt

401 revisited

Post by mido »

I get an invalid memory reference with this. Please check it out.. :-? :
[cpp]
#include <iostream.h>
#include <string.h>

char* str;
char char_arr[] = "AEHIJLMOSTUVWXYZ12358";
char rev_arr[] = "A3HILJMO2TUVWXY51SEZ8";

void read_str(char*& x)
{
char *temp,*temp2;
char ch;
while (!cin.eof() && cin.get(ch) && ch!='\n')
{
if (strlen(x)>0)
{
temp= new char[strlen(x)];
temp[0]=0;
strcpy(temp,x);
x=new char[strlen(temp)+1];
x="";
x=temp;
temp2=new char[1];
*temp2=ch;
temp2[1]=0;
strcat(x,temp2);
}
else
{
x=new char[1];
temp2=new char[1];
*temp2=ch;
temp2[1]=0;
x[0]=0;
strcat(x,temp2);
}
}
}

bool Check_P(char*& x)
{
int i;
for (i=0;i<(strlen(x)/2);i++)
if (x!=x[strlen(x)-i-1])
return false;
return true;
}

bool Check_Rev(char*& x)
{
int l,m;
for (l=0;l<strlen(x);l++)
{
for (m=0;m<21;m++)
{
if (x[l]==char_arr[m])
break;
}
if (m==21)
break;
}
if (l!=strlen(x))
return false;
for (l=0;l<(strlen(x)/2);l++)
{
for (m=0;m<21;m++)
if (x[l]==char_arr[m])
break;
if (rev_arr[m]!=x[strlen(x)-l-1])
return false;
}
return true;
}


void Check_All(char*& x)
{
if (Check_P(str))
{
if (Check_Rev(str))
cout<<"is a mirrored palindrome."<<endl<<endl;
else
cout<<"is a regular palindrome."<<endl<<endl;
}
else if (Check_Rev(str))
{
cout<<"is a mirrored string."<<endl<<endl;
}
else
cout<<"is not a palindrome."<<endl<<endl;
}

void main()
{
while (!cin.eof())
{
str=new char[0];
str="";
read_str(str);
if (str!="")
{
cout<<str<<" -- ";
Check_All(str);
}
}
}
[/cpp]
mido
Learning poster
Posts: 78
Joined: Sun Jun 16, 2002 9:48 pm
Location: Cairo,Egypt

Post by mido »

Here's a cleaner one. This got me a wrong answer..Any tips?:
[cpp]
#include <iostream.h>
#include <string.h>

char *str;
char char_arr[] = "AEHIJLMOSTUVWXYZ12358";
char rev_arr[] = "A3HILJMO2TUVWXY51SEZ8";

bool Check_P(char*& x)
{
int i;
for (i=0;i<(strlen(x)/2);i++)
if (x!=x[strlen(x)-i-1])
return false;
return true;
}

bool Check_Rev(char*& x)
{
int l,m;
for (l=0;l<strlen(x)/2;l++)
{
for (m=0;m<21;m++)
{
if (x[l]==char_arr[m])
break;
}
if (m==21)
break;
}
if (l!=strlen(x)/2)
return false;
for (l=0;l<(strlen(x)/2);l++)
{
for (m=0;m<21;m++)
if (x[l]==char_arr[m])
break;
if (rev_arr[m]!=x[strlen(x)-l-1])
return false;
}
return true;
}


void Check_All(char*& x)
{
if (Check_P(str))
{
if (Check_Rev(str))
cout<<"is a mirrored palindrome."<<endl<<endl;
else
cout<<"is a regular palindrome."<<endl<<endl;
}
else if (Check_Rev(str))
{
cout<<"is a mirrored string."<<endl<<endl;
}
else
cout<<"is not a palindrome."<<endl<<endl;
}

void main()
{
str=new char[10000000];
while (cin>>str)
{
if (str!="")
{
cout<<str<<" -- ";
Check_All(str);
}
}
}
[/cpp]
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN »

I think it's a bug when checking with a mirrored string which size is an odd number and the middle character is not a mirror character. i.e. for example try
AAAAAGAAAAA

Just modify a bit for your for loop I think it should be OK.
mido
Learning poster
Posts: 78
Joined: Sun Jun 16, 2002 9:48 pm
Location: Cairo,Egypt

Post by mido »

Bull's eye, amigo. Thanks a lot. :wink:
cyfra
Experienced poster
Posts: 144
Joined: Thu Nov 22, 2001 2:00 am
Location: Gdynia, Poland

Hi!

Post by cyfra »

Hi!

Damn... this is the next situation when I got somebody's program Accepted without any changes....

Try to change your mail client (look at Misc section for more information)...

Many mail clients (Outlook) cut lines in programs and that's why you have WA..

So for the next time try to send your program in other way...

Good Luck :wink:

PS.
While writing a post on forum try to use quotes for your code ....
It is much easier for reading :D
udcp
New poster
Posts: 6
Joined: Fri Jun 21, 2002 10:24 pm
Location: USA

Post by udcp »

I have sent the code as plain text... Still I get WA!!!!!!!!
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN »

As cyfra said your program can be got accepted without any change. That's your submission problem.

You can try to add a line of
/* @BEGIN_OF_SOURCE_CODE */ as the 1st line of your program and add a line of
/* @END_OF_SOURCE_CODE */ as the last line of your program to see if it helps
Post Reply

Return to “Volume 4 (400-499)”