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
Lotendil
New poster
Posts: 3
Joined: Thu Feb 16, 2006 1:12 am

Post by Lotendil »

yes, I try all the tests in all the posts but I don't find a good solution with my problem

There is a problem in the .ps file. It doesn't contain the reverse letter of 'S', but it do appears in the html version.

tmdrbs6584
Learning poster
Posts: 98
Joined: Sat Jan 21, 2006 12:45 pm
Location: Busan,Corea(Republic of)

Post by tmdrbs6584 »

palindrome...

tmdrbs6584
Learning poster
Posts: 98
Joined: Sat Jan 21, 2006 12:45 pm
Location: Busan,Corea(Republic of)

Post by tmdrbs6584 »

palindrome...

tmdrbs6584
Learning poster
Posts: 98
Joined: Sat Jan 21, 2006 12:45 pm
Location: Busan,Corea(Republic of)

Post by tmdrbs6584 »

palindrome...

Godzio
New poster
Posts: 2
Joined: Sun Mar 26, 2006 8:17 pm

401 Another idiot asking for help...

Post by Godzio »

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

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.

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Your code looks ok to me. Did you try testing your code with a string of exactly 20 characters? Your char array has a size 20, but you need a size of 21 to hold the NULL character as well as to hold a string of length 20. This is in the only problem I see.

Godzio
New poster
Posts: 2
Joined: Sun Mar 26, 2006 8:17 pm

Post by Godzio »

@UP
thanks for spoting the bug but something is still wrong.. I`m short on luck.
If any one else spots something give me a hint :D

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

One more thing I did not point out is that you are checking the return value of scanf with EOF. Try changing that to

Code: Select all

while(scanf("%s",ciag) == 1)
Checking with EOF can sometimes give problems as the last input might only have a end-of-line rather than end-of-file.

farzane
New poster
Posts: 26
Joined: Thu Jun 15, 2006 9:26 am

401(please help)

Post by farzane »

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;
}
}

emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

It is a WA code

jan_holmes
Experienced poster
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore
Contact:

Post by jan_holmes »

Yes,I think it's a WA Solution... Something might be wrong in your solution...

farzane
New poster
Posts: 26
Joined: Thu Jun 15, 2006 9:26 am

Post by farzane »

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;
}
}

jan_holmes
Experienced poster
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore
Contact:

Post by jan_holmes »

Here I Changed your code a bit,and got AC.

Code: Select all

removed...
Hope it helps... :)
Last edited by jan_holmes on Sat Jun 17, 2006 10:47 am, edited 1 time in total.

emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

Giving ACCEPTED or PRESENTATION ERROR code in forum spoils the problem.

So I request to remove those codes.

mhayter1
New poster
Posts: 15
Joined: Wed Jul 26, 2006 10:00 am

Palindromes problems (401)

Post by mhayter1 »

Given the test cases available on the forum, I think I only got one wrong.
This is my output:
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.
my code is as follows:

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;
}
        
I would greatly appreciate any help I can get. Thanks!

Post Reply

Return to “Volume 4 (400-499)”