Page 1 of 14

401 - Palindromes

Posted: Tue Apr 16, 2002 9:31 pm
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]

Help for 401

Posted: Thu Jun 20, 2002 7:32 pm
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]

Posted: Thu Jun 20, 2002 7:58 pm
by xenon
[c] rev['F'] = '8';[/c]
The shift-key gets sticky if you use it often...

Posted: Fri Jun 21, 2002 3:28 am
by Melon Melon
Sorry...
i dont understand what u mean....

Posted: Fri Jun 21, 2002 3:40 am
by 10153EN
I think Xeron means the line should be *shift-8*, but not *8* alone. i.e.


[c] rev['F'] = '*'; [/c]

Posted: Fri Jun 21, 2002 5:22 am
by Melon Melon
i got accepted..
Ths a lot!!^^

401 : Can anyone help me??

Posted: Fri Jun 21, 2002 10:36 pm
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"*/

Help me correct the error??

Posted: Sat Jun 22, 2002 12:09 am
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]

401 revisited

Posted: Sat Jun 22, 2002 10:48 pm
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]

Posted: Sat Jun 22, 2002 11:40 pm
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]

Posted: Sun Jun 23, 2002 5:38 am
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.

Posted: Sun Jun 23, 2002 3:27 pm
by mido
Bull's eye, amigo. Thanks a lot. :wink:

Hi!

Posted: Mon Jun 24, 2002 4:46 pm
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

Posted: Mon Jun 24, 2002 6:57 pm
by udcp
I have sent the code as plain text... Still I get WA!!!!!!!!

Posted: Mon Jun 24, 2002 7:21 pm
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