401 - Palindromes
Moderator: Board moderators
-
- Learning poster
- Posts: 98
- Joined: Sat Jan 21, 2006 12:45 pm
- Location: Busan,Corea(Republic of)
palindrome...
Archaan
CAN YOU BEAT ME?
http://acm.uva.es/problemset/usersjudge.php?user=19788
AND,
http://acm.uva.es/problemset/submit.php
http://online-judge.uva.es/problemset/submit.php
SUBMIT AND GET AC!!!
CAN YOU BEAT ME?
http://acm.uva.es/problemset/usersjudge.php?user=19788
AND,
http://acm.uva.es/problemset/submit.php
http://online-judge.uva.es/problemset/submit.php
SUBMIT AND GET AC!!!
-
- Learning poster
- Posts: 98
- Joined: Sat Jan 21, 2006 12:45 pm
- Location: Busan,Corea(Republic of)
palindrome...
Archaan
CAN YOU BEAT ME?
http://acm.uva.es/problemset/usersjudge.php?user=19788
AND,
http://acm.uva.es/problemset/submit.php
http://online-judge.uva.es/problemset/submit.php
SUBMIT AND GET AC!!!
CAN YOU BEAT ME?
http://acm.uva.es/problemset/usersjudge.php?user=19788
AND,
http://acm.uva.es/problemset/submit.php
http://online-judge.uva.es/problemset/submit.php
SUBMIT AND GET AC!!!
-
- Learning poster
- Posts: 98
- Joined: Sat Jan 21, 2006 12:45 pm
- Location: Busan,Corea(Republic of)
palindrome...
Archaan
CAN YOU BEAT ME?
http://acm.uva.es/problemset/usersjudge.php?user=19788
AND,
http://acm.uva.es/problemset/submit.php
http://online-judge.uva.es/problemset/submit.php
SUBMIT AND GET AC!!!
CAN YOU BEAT ME?
http://acm.uva.es/problemset/usersjudge.php?user=19788
AND,
http://acm.uva.es/problemset/submit.php
http://online-judge.uva.es/problemset/submit.php
SUBMIT AND GET AC!!!
401 Another idiot asking for help...
Well i have been working on this problem for a while now and it gives the correct anwser for all the test i have found on this forum but yet i get the damn WA...
If you find any mistakes or know any tests that make my program work wrong HELP ME !! I will be gratefull ;D
If you find any mistakes or know any tests that make my program work wrong HELP ME !! I will be gratefull ;D
Code: Select all
#include<cstdio>
#include<string>
using namespace std;
char ciag[22];
char mirrored[22];
bool par=true;
bool mir=true;
bool palindrome_test()
{
int i,j;
bool odp=true;
for(i=0,j=strlen(ciag)-1;i<=strlen(ciag)-1,j>=0;++i,--j)
{
switch(ciag[i])
{
case 'E':
mirrored[i]='3';
break;
case 'J':
mirrored[i]='L';
break;
case 'L':
mirrored[i]='J';
break;
case 'S':
mirrored[i]='2';
break;
case 'Z':
mirrored[i]='5';
break;
case '2':
mirrored[i]='S';
break;
case '3':
mirrored[i]='E';
break;
case '5':
mirrored[i]='Z';
break;
default:
mirrored[i]=ciag[i];
break;
}
if(ciag[i]!=ciag[j])
{odp=false;
}
}
return odp;
}
bool mirrored_test()
{
int i,j;
bool odp=true;
for(i=0,j=strlen(ciag)-1;i<=strlen(ciag)-1,j>=0;++i,--j)
{
if(mirrored[i]=='B' ||mirrored[i]=='C' || mirrored[i]=='D' || mirrored[i]=='F' || mirrored[i]=='G' || mirrored[i]=='K' || mirrored[i]=='N' || mirrored[i]=='P' || mirrored[i]=='Q' || mirrored[i]=='R' || mirrored[i]=='4' || mirrored[i]=='6' || mirrored[i]=='7' || mirrored[i]=='9')
{ odp=false;
break;}
if(ciag[i]!=mirrored[j])
{ odp=false;
break;}
}
return odp;
}
int main()
{
while(scanf("%s",&ciag) != EOF)
{
par=palindrome_test();
mir=mirrored_test();
if(par==true && mir==true)
printf("%s -- is a mirrored palindrome.\n\n",ciag);
if(par==true && mir==false)
printf("%s -- is a regular palindrome.\n\n",ciag);
if(par==false && mir==true)
printf("%s -- is a mirrored string.\n\n",ciag);
if(par==false && mir==false)
printf("%s -- is a not a palindrome.\n\n",ciag);
}
return 0;
}
Last edited by Godzio on Mon Mar 27, 2006 6:02 pm, edited 1 time in total.
One more thing I did not point out is that you are checking the return value of scanf with EOF. Try changing that to
Checking with EOF can sometimes give problems as the last input might only have a end-of-line rather than end-of-file.
Code: Select all
while(scanf("%s",ciag) == 1)
401(please help)
Hi dear all;
I sent this code for problem 401"Palindromes" and I recieved a presentation error.
could any one help me and say what's wrong whit this code.
I'd realy appreciate you.
Regard
-----------------------code is here---------------------------
#include<iostream.h>
void fstrcpy(char *des,char *sor){
int i;
for(i=0;sor;i++)
des=sor;
des=sor;
}
int fstrlen(char *str){
int i=0;
for(;str;i++);
return i;
}
void fstrrev(char *str){
int i=0;
int j=0;
for(;str[j];j++);
j--;
char temp;
while(i<=j){
temp=str;
str=str[j];
str[j]=temp;
i++;j--;
}
}
char *fstrchr(char *str,char chr){
while(*str){
if((*str)==chr)return str;
str++;
}
return NULL;
}
int fstrcmp(char *str1,char *str2){
int i=0;
while(str1){
if(str1<str2[i])return -1;
if(str1[i]>str2[i])return 1;
i++;
}
if(str1[i]<str2[i])return -1;
if(str1[i]>str2[i])return 1;
return 0;
}
void main(){
char s[30],srev[30],mirror[30];
int psw,msw,nmsw;
int i,place,len;
char mir[2][40]={"AEHIJLMOSTUVWXYZ12358","A3HILJMO2TUVWXY51SEZ8"};
while(cin>>s){
psw=msw=0;
nmsw=0;
fstrcpy(srev,s);
fstrrev(srev);
if(fstrcmp(s,srev)==0)psw=1;
len=fstrlen(s);
for(i=0;i<len;i++){
if(fstrchr(mir[0],s[i])!=NULL)
place=(fstrchr(mir[0],s[i])-mir[0]);
else place=-1;
if(place<0)(nmsw=1);
else mirror[i]=mir[1][place];
}
mirror[i]='\0';
if(nmsw==0){
fstrcpy(srev,mirror);
fstrrev(srev);
if(fstrcmp(s,srev)==0)msw=1;
}
if(psw==0 && msw==0)cout<<s<<" -- is not a palindrome."<<endl;
else if(psw==1 && msw==0)cout<<s<<" -- is a regular
palindrome."<<endl;
else if(psw==0 && msw==1)cout<<s<<" -- is a mirrored string."<<endl;
else if(psw==1 && msw==1)cout<<s<<" -- is a mirrored
palindrome."<<endl;
}
}
I sent this code for problem 401"Palindromes" and I recieved a presentation error.
could any one help me and say what's wrong whit this code.
I'd realy appreciate you.
Regard
-----------------------code is here---------------------------
#include<iostream.h>
void fstrcpy(char *des,char *sor){
int i;
for(i=0;sor;i++)
des=sor;
des=sor;
}
int fstrlen(char *str){
int i=0;
for(;str;i++);
return i;
}
void fstrrev(char *str){
int i=0;
int j=0;
for(;str[j];j++);
j--;
char temp;
while(i<=j){
temp=str;
str=str[j];
str[j]=temp;
i++;j--;
}
}
char *fstrchr(char *str,char chr){
while(*str){
if((*str)==chr)return str;
str++;
}
return NULL;
}
int fstrcmp(char *str1,char *str2){
int i=0;
while(str1){
if(str1<str2[i])return -1;
if(str1[i]>str2[i])return 1;
i++;
}
if(str1[i]<str2[i])return -1;
if(str1[i]>str2[i])return 1;
return 0;
}
void main(){
char s[30],srev[30],mirror[30];
int psw,msw,nmsw;
int i,place,len;
char mir[2][40]={"AEHIJLMOSTUVWXYZ12358","A3HILJMO2TUVWXY51SEZ8"};
while(cin>>s){
psw=msw=0;
nmsw=0;
fstrcpy(srev,s);
fstrrev(srev);
if(fstrcmp(s,srev)==0)psw=1;
len=fstrlen(s);
for(i=0;i<len;i++){
if(fstrchr(mir[0],s[i])!=NULL)
place=(fstrchr(mir[0],s[i])-mir[0]);
else place=-1;
if(place<0)(nmsw=1);
else mirror[i]=mir[1][place];
}
mirror[i]='\0';
if(nmsw==0){
fstrcpy(srev,mirror);
fstrrev(srev);
if(fstrcmp(s,srev)==0)msw=1;
}
if(psw==0 && msw==0)cout<<s<<" -- is not a palindrome."<<endl;
else if(psw==1 && msw==0)cout<<s<<" -- is a regular
palindrome."<<endl;
else if(psw==0 && msw==1)cout<<s<<" -- is a mirrored string."<<endl;
else if(psw==1 && msw==1)cout<<s<<" -- is a mirrored
palindrome."<<endl;
}
}
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
-
- Experienced poster
- Posts: 136
- Joined: Fri Apr 15, 2005 3:47 pm
- Location: Singapore
- Contact:
but I got a presentation error
here I copy_past my code from my email that after that I recived presentation error.Maybe last time I put another code.If so I'm realy sorry.
here it is:
You are trying to solve "Palindromes" (problem 401).
I have received and stored your C++ program. It will be compiled and
run
as soon as possible; please be patient waiting for the results...
--
The Online Judge
--------------- The program I'll compile begins here: ---------------
#include<iostream.h>
void fstrcpy(char *des,char *sor){
int i;
for(i=0;sor;i++)
des=sor;
des=sor;
}
int fstrlen(char *str){
int i=0;
for(;str;i++);
return i;
}
void fstrrev(char *str){
int i=0;
int j=0;
for(;str[j];j++);
j--;
char temp;
while(i<=j){
temp=str;
str=str[j];
str[j]=temp;
i++;j--;
}
}
char *fstrchr(char *str,char chr){
while(*str){
if((*str)==chr)return str;
str++;
}
return NULL;
}
int fstrcmp(char *str1,char *str2){
int i=0;
while(str1){
if(str1<str2[i])return -1;
if(str1[i]>str2[i])return 1;
i++;
}
if(str1[i]<str2[i])return -1;
if(str1[i]>str2[i])return 1;
return 0;
}
void main(){
char s[30],srev[30],mirror[30];
int psw,msw,nmsw;
int i,place,len;
char mir[2][40]={"AEHIJLMOSTUVWXYZ12358","A3HILJMO2TUVWXY51SEZ8"};
while(cin>>s){
psw=msw=0;
nmsw=0;
fstrcpy(srev,s);
fstrrev(srev);
if(fstrcmp(s,srev)==0)psw=1;
len=fstrlen(s);
for(i=0;i<len;i++){
if(fstrchr(mir[0],s[i])!=NULL)
place=(fstrchr(mir[0],s[i])-mir[0]);
else place=-1;
if(place<0)(nmsw=1);
else mirror[i]=mir[1][place];
}
mirror[i]='\0';
if(nmsw==0){
fstrcpy(srev,mirror);
fstrrev(srev);
if(fstrcmp(s,srev)==0)msw=1;
}
if(psw==0 && msw==0)cout<<s<<" -- is not a palindrome."<<endl;
else if(psw==1 && msw==0)cout<<s<<" -- is a regular plindrome."<<endl;
else if(psw==0 && msw==1)cout<<s<<" -- is a mirrored string."<<endl;
else if(psw==1 && msw==1)cout<<s<<" -- is a mirrored
palindrome."<<endl;
}
}
here I copy_past my code from my email that after that I recived presentation error.Maybe last time I put another code.If so I'm realy sorry.
here it is:
You are trying to solve "Palindromes" (problem 401).
I have received and stored your C++ program. It will be compiled and
run
as soon as possible; please be patient waiting for the results...
--
The Online Judge
--------------- The program I'll compile begins here: ---------------
#include<iostream.h>
void fstrcpy(char *des,char *sor){
int i;
for(i=0;sor;i++)
des=sor;
des=sor;
}
int fstrlen(char *str){
int i=0;
for(;str;i++);
return i;
}
void fstrrev(char *str){
int i=0;
int j=0;
for(;str[j];j++);
j--;
char temp;
while(i<=j){
temp=str;
str=str[j];
str[j]=temp;
i++;j--;
}
}
char *fstrchr(char *str,char chr){
while(*str){
if((*str)==chr)return str;
str++;
}
return NULL;
}
int fstrcmp(char *str1,char *str2){
int i=0;
while(str1){
if(str1<str2[i])return -1;
if(str1[i]>str2[i])return 1;
i++;
}
if(str1[i]<str2[i])return -1;
if(str1[i]>str2[i])return 1;
return 0;
}
void main(){
char s[30],srev[30],mirror[30];
int psw,msw,nmsw;
int i,place,len;
char mir[2][40]={"AEHIJLMOSTUVWXYZ12358","A3HILJMO2TUVWXY51SEZ8"};
while(cin>>s){
psw=msw=0;
nmsw=0;
fstrcpy(srev,s);
fstrrev(srev);
if(fstrcmp(s,srev)==0)psw=1;
len=fstrlen(s);
for(i=0;i<len;i++){
if(fstrchr(mir[0],s[i])!=NULL)
place=(fstrchr(mir[0],s[i])-mir[0]);
else place=-1;
if(place<0)(nmsw=1);
else mirror[i]=mir[1][place];
}
mirror[i]='\0';
if(nmsw==0){
fstrcpy(srev,mirror);
fstrrev(srev);
if(fstrcmp(s,srev)==0)msw=1;
}
if(psw==0 && msw==0)cout<<s<<" -- is not a palindrome."<<endl;
else if(psw==1 && msw==0)cout<<s<<" -- is a regular plindrome."<<endl;
else if(psw==0 && msw==1)cout<<s<<" -- is a mirrored string."<<endl;
else if(psw==1 && msw==1)cout<<s<<" -- is a mirrored
palindrome."<<endl;
}
}
-
- Experienced poster
- Posts: 136
- Joined: Fri Apr 15, 2005 3:47 pm
- Location: Singapore
- Contact:
Last edited by jan_holmes on Sat Jun 17, 2006 10:47 am, edited 1 time in total.
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
Palindromes problems (401)
Given the test cases available on the forum, I think I only got one wrong.
This is my output:
I would greatly appreciate any help I can get. Thanks!
This is my output:
my code is as follows:NOTAPALINDROME -- is not a palindrome.
ISAPALINILAPASI -- is a regular palindrome.
2A3MEAS -- is a mirrored string.
ATOYOTA -- is a mirrored palindrome.
MAIAM -- is a mirrored palindrome.
123ESI -- is not a palindrome.
123ES1 -- is a mirrored string.
DEO3D -- is a mirrored string.//is not a palindrome
9339 -- is a regular palindrome.
A -- is a mirrored palindrome.
B -- is a regular palindrome.
E -- is a regular palindrome.
M -- is a mirrored palindrome.
1 -- is a mirrored palindrome.
3 -- is a regular palindrome.
2 -- is a regular palindrome.
4 -- is a regular palindrome.
Code: Select all
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<fstream>
#include<cstdio>
using namespace std;
ofstream fout("C:\\output.txt");
char lookup[130]={'\0'};
bool isPal(string &str1)
{
for(int i=0;i<(str1.size())/2;i++)
if(str1[i]!=str1[str1.size()-1-i]) return 0;
return 1;
}
bool isMirrored(string str1)
{
char mirror[25]={'\0'};
bool changed=0;
for(int i=0;i<str1.size();i++)
{
if(lookup[str1[i]]=='\0')
{
mirror[i]=str1[i];
}
else if(lookup[str1[i]]!='\0')
{
mirror[i]=lookup[str1[i]];
changed=1;
}
}
reverse(mirror,mirror+strlen(mirror));
//cout << "mirror= " << mirror << " str1= " << str1 << endl;
if(mirror==str1 && changed)
return 1;
return 0;
}
int main()
{
string str1;
bool pal, mirrored;
lookup['A']='A';
lookup['E']='3';
lookup['H']='H';
lookup['I']='I';
lookup['J']='L';
lookup['L']='J';
lookup['M']='M';
lookup['O']='O';
lookup['S']='2';
lookup['T']='T';
lookup['U']='U';
lookup['V']='V';
lookup['W']='W';
lookup['X']='X';
lookup['Y']='Y';
lookup['Z']='5';
lookup['1']='1';
lookup['2']='S';
lookup['3']='E';
lookup['5']='Z';
lookup['8']='8';
while(cin>>str1)
{
pal=isPal(str1);
mirrored=isMirrored(str1);
fout<< str1;
if(!pal && !mirrored) fout<<" -- is not a palindrome." << endl;
else if(pal && !mirrored) fout<<" -- is a regular palindrome."<< endl;
else if(!pal && mirrored) fout<<" -- is a mirrored string."<<endl;
else if(pal && mirrored)fout<< " -- is a mirrored palindrome." << endl;
fout<< endl;
}
return 0;
}