401 - Palindromes
Moderator: Board moderators
401 - Palindromes
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]
[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]
-
- New poster
- Posts: 17
- Joined: Fri May 31, 2002 6:30 pm
- Contact:
Help for 401
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]
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]
-
- New poster
- Posts: 17
- Joined: Fri May 31, 2002 6:30 pm
- Contact:
-
- New poster
- Posts: 17
- Joined: Fri May 31, 2002 6:30 pm
- Contact:
401 : Can anyone help me??
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"*/
![:(](./images/smilies/icon_frown.gif)
/*"@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??
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]
[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
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]
![:-?](./images/smilies/icon_confused.gif)
[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]
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]
[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]
Hi!
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:](./images/smilies/icon_wink.gif)
PS.
While writing a post on forum try to use quotes for your code ....
It is much easier for reading![:D](./images/smilies/icon_biggrin.gif)
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:](./images/smilies/icon_wink.gif)
PS.
While writing a post on forum try to use quotes for your code ....
It is much easier for reading
![:D](./images/smilies/icon_biggrin.gif)