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

udcp
New poster
Posts: 6
Joined: Fri Jun 21, 2002 10:24 pm
Location: USA

Post by udcp » Mon Jun 24, 2002 8:46 pm

I got it ACCEPTED!!!!!!! :D
The only change I made was changed
' int main(int argv, char *argc[]) '
to
'void main()'

But.. Can't we actually use command line args while submitting our program?? :-?

10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN » Mon Jun 24, 2002 9:22 pm

What do mean by *command line args*?

I also write the programs as
int main(int argc, char **argv)
and it's OK~

cyfra
Experienced poster
Posts: 144
Joined: Thu Nov 22, 2001 2:00 am
Location: Gdynia, Poland

Post by cyfra » Tue Jun 25, 2002 12:21 pm

Hi!

udcp: what mail client do you use ??

Maybe the problem is there because I don't believe that you couldn't get your program AC without any changes while other persons could...

That's the next strange case in Online Judge history :wink:

Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen » Sat Jul 13, 2002 10:40 pm

the following code of mine also got W.A. :(
can anyone find the bug??? :wink:

Code: Select all

#include <stdio.h>
#include <string.h>

char in[50];

void check()
{
   char a1[] = {'A', 'E', 'H', 'I', 'J', 'L', 'M', 'O', 'S', 'T', 'U', 'V', 'W',
   'X', 'Y', 'Z', '1', '2', '3', '5', '8'};
   char a2[] = {'A', '3', 'H', 'I', 'L', 'J', 'M', 'O', '2', 'T', 'U', 'V', 'W',
   'X', 'Y', '5', '1', 'S', 'E', 'Z', '8'};

   int len = strlen(in);
   int i, j, k, half;
   int pdrome = 1, mirror = 1;

   half = len / 2;
   for(i=0, j=len-1; i<half; i++, j--)
   {
   	if(in[i]!=in[j]) pdrome = 0;
      for(k=0; k<21; k++)
      {
      	if(in[i]==a1[k])
         {
           	if(in[j]!=a2[k])
            {
              	mirror = 0;
               break;
            }
         }
      }
   }

   if(len%2==1 && mirror)
   {
      mirror = 0;
   	i = half;
      for(k=0; k<21; k++)
      {
      	if(in[i]==a1[k])
         {
         	if(a1[k]==a2[k])
            	mirror = 1;
            break;
         }
      }
   }

   if(pdrome && mirror)
   	printf("%s -- is a mirrored palindrome.\n\n", in);
   else if(pdrome)
   	printf("%s -- is a regular palindrome.\n\n", in);
   else if(mirror)
   	printf("%s -- is a mirrored string.\n\n", in);
   else
      printf("%s -- is not a palindrome.\n\n", in);
}

void main()
{
   while(scanf("%s", in)==1)
   {
   	check();
   }
}
I couldn't find the bug, but solved it using another approach.
Last edited by Subeen on Tue Jul 20, 2004 1:53 pm, edited 1 time in total.

kuasha
New poster
Posts: 8
Joined: Sat Jul 06, 2002 3:46 pm
Location: CSE, SUST, Bangladesh
Contact:

Post by kuasha » Mon Jul 15, 2002 5:18 pm

I dont understand why this simple problem cause so much problem. I have tried almost everything I can imagine. But the WA is not leaving me.

Melon Melon
New poster
Posts: 17
Joined: Fri May 31, 2002 6:30 pm
Contact:

Post by Melon Melon » Mon Jul 15, 2002 6:50 pm

mm...
yes..i ve tried subeen's one...but still cant get where get wrong...
and for udcp's one..
i ve sent to the judge...it got accepted..
....
so...strange..for both..-__-"

masti
New poster
Posts: 3
Joined: Mon Aug 05, 2002 11:03 pm

401 - complie error [pascal] :-?

Post by masti » Mon Aug 05, 2002 11:07 pm

I have some problems with this code. When I'am compiling it under Turbo Pascal everything is Ok but in Free Pascal I get errors. Can anyone help me?
**********************************************************
program palindromes(input,output);


const
pali = ' -- is a regular palindrome.';
mstrin = ' -- is a mirrored string.';
mpali = ' -- is a mirrored palindrome.';
none = ' -- is not a palindrome.';

var
P:array [1..42] of char;
s:string;
i:integer;


procedure check;

label
j1,j2,j3;

var
i,n:integer;
l:integer;
is:boolean;

begin
l:=length(s);
n:=l div 2;
is:=true;
for i:=1 to n do
if s <> s[l-i+1] then
begin
is:=false;
goto j1;
end;
j1:
if is then
begin
for i:=1 to n do
if s <> P[ord(s[l-i+1])-48] then
begin
is:=false;
goto j2;
end;
j2:
if is then
writeln(output,s,mpali)
else
writeln(output,s,pali);
end
else
begin
is:=true;
for i:=1 to n do
if s <> P[ord(s[l-i+1])-48] then
begin
is:=false;
goto j3;
end;
j3:
if is then
writeln(s,mstrin)
else
writeln(s,none);
end;
end;

begin
P[1]:='1';
P[2]:='S';
P[3]:='E';
P[5]:='Z';
P[8]:='8';
P[17]:='A';
P[21]:='3';
P[24]:='H';
P[25]:='I';
P[26]:='L';
P[28]:='J';
P[29]:='M';
P[31]:='O';
P[35]:='2';
P[36]:='T';
P[37]:='U';
P[38]:='V';
P[39]:='W';
P[40]:='X';
P[41]:='Y';
P[42]:='5';


while not eof(input) do
begin
readln(input,s);
check;
writeln(output);
end;
end.[/pascal]

liusu
New poster
Posts: 22
Joined: Thu Aug 01, 2002 10:26 am

got?

Post by liusu » Sat Aug 10, 2002 1:37 pm

You'd better not use 'goto' sentence.it's not a good ideal.I think if there is no 'goto',the error will go away.

good luck.

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

Post by xenon » Sat Aug 10, 2002 2:52 pm

Some remarks:

1. Use Pascal button to format your code. This makes it much more readable.

2. Your program compiles perfectly with my free pascal compiler, but according to the standard, when using labels within a procedure, first declare the variables, then declare the labels. Like this:
[pascal]procedure bla(bla:bla);
var
bla:bla;
label
bla;
begin
bla;
bla:
bla;
end;[/pascal]
And NOT
[pascal]procedure bla(bla:bla);
label
bla;
var
bla:bla;
begin
bla;
bla:
bla;
end;[/pascal]
The Judge's Pascal compiler is very strict, so that could be the problem.

3. I'm not too manic about avoiding GOTO statements, but this is a little too much. Use the break statement if you want to leave a loop prematurely:
[pascal]for bla:=bla to bla do begin
if (bla=bla) then break;
bla;
end;[/pascal]
Instead of:
[pascal]for bla:=bla to bla do begin
if (bla=bla) then goto bla;
bla;
end;
bla:[/pascal]
Of course this doesn't work if you want to break out of more then one level of looping. But if you need that, most likely there is something messed up with your algorithm, and you can better redesign the program.

Hope it helps,
-xenon

liusu
New poster
Posts: 22
Joined: Thu Aug 01, 2002 10:26 am

you can do like this:

Post by liusu » Sat Aug 10, 2002 5:29 pm

you can use the 'while' :

[pascal]flag:=true;
i:=xx;
while (i<xxx) and flag do
begin
xxxx;
if xxx then flag:=false
else inc(i);
end;[/pascal]

I used it and pass this problem.good luck. :wink:

masti
New poster
Posts: 3
Joined: Mon Aug 05, 2002 11:03 pm

Post by masti » Sun Aug 11, 2002 9:41 pm

thanks for help I will try redesing my problem as you said

ashfaq_csdu
New poster
Posts: 4
Joined: Mon Mar 18, 2002 2:00 am

problem in 401

Post by ashfaq_csdu » Tue Oct 22, 2002 9:45 pm

hi,
I am getting WA for 6 times in this problem. I think I am missing something.
Q1. What happened if the string is of only 1 letter?
Q2. Can the string be of small letters?
Q3. If there is no reverse then what happens? Will it say this is not mirrored string or mirrored palindrome? Or will it ignore it?

Please help me.

Ashfaq

User avatar
Angel
New poster
Posts: 8
Joined: Wed Nov 20, 2002 3:49 pm

401 WA: Help mee...

Post by Angel » Wed Nov 20, 2002 4:04 pm

first debug ......... then run it : see the diffrence.........plz help me.......







#include<stdio.h>
#include<conio.h>
#include<string.h>

int palindrome();
int mirror();
char chart(char ch);
char A[22],B[22];
char C[14]={'A','H','I','M','O','T','U','V','W','X','Y','1','8'};
char D[10]={'J','L','S','2','Z','5','3','E'};

void main(void)
{

while(1)
{
int palin=0,mir=0;
gets(A);
palin=palindrome();
mir=mirror();
if(palin==1&&mir==1){printf("%s - is a mirrored palindrome.\n",A);printf("\n");}
else if(palin==1&&mir!=1){printf("%s - is a regular palindrome.\n",A);printf("\n");}
else if(palin!=1&&mir==1){printf("%s - is a mirrored string.\n",A);printf("\n");}
else if(palin!=1&&mir!=1){printf("%s - is not a palindrome.\n",A);printf("\n");}

memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
}
}

int mirror()
{
int flag,flag2,a,c,d,count=0,count2=0,len;
char ch;
len=strlen(A);
for(a=0;A[a];a++)
{
if(flag==0)break;

for(c=0;c<13;c++)
{
if(A[a]==C[c])
{
count++;
if(A[a]==A[len-a-1])
flag=1;
else flag=0;
}
if(flag==0)break;
}
}



for(a=0;A[a];a++)
{
if(flag2==0)break;
for(d=0;d<9;d++)
{
if(D[d]==A[a])
{
count2++;
ch=A[a];
ch=chart(ch);
if(A[len-1-a]==ch)
{
flag2=1;
}
else{flag2=0;break;}
}
}
}

if(flag==1&&flag2==1)return 1;
if(count==0&&flag2==1)return 1;
if(flag==1&&count2==0)return 1;
else return 0;
}

int palindrome()
{
int b=0,cmp,a,len;
len=strlen(A);
strcpy(B,A);
for(a=len-1;a>=0;a--)B[b++]=A[a];
cmp=strcmp(A,B);
if(cmp==0)return 1;
else return 0;
}

char chart(char ch)
{
if(ch=='J')ch='L';
else if(ch=='L')ch='J';
else if(ch=='3')ch='E';
else if(ch=='E')ch='3';
else if(ch=='S')ch='2';
else if(ch=='2')ch='S';
else if(ch=='5')ch='Z';
else if(ch=='Z')ch='5';

return ch;
}

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Wed Nov 20, 2002 4:56 pm

no "regular" word in output ....

Best regards
Dominik

User avatar
rakeb
New poster
Posts: 42
Joined: Fri Aug 30, 2002 2:51 pm
Location: France

Post by rakeb » Thu Nov 21, 2002 2:10 pm

i think you done too much to chek the mirror
just define normal and mirrored charecters in two strings
[cpp]
char Char[22]="AEHIJLMOSTUVWXYZ12358";
char Revers[22]="A3HILJMO2TUVWXY51SEZ8";

[/cpp]
then build another string b[100] by the mirrored charecters of original charecters. then compare them, if they are same then the string is mirrored.
[cpp]

for(i=0,j=n-1;i<n;i++,j--){
b=search(m[j]);
}
b[n]='\0';
if ( !strcmp(m,b))return 1;
[/cpp]
in the search function just return the corrosponding mirrored charecter
by searching Char and Revers string

Post Reply

Return to “Volume 4 (400-499)”