444 - Encoder and Decoder

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

razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

Tell me Why 444 always Tme limit exceeds?

Post by razibcse »

Code: Select all

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define MAX 100
void encode(char mas[],long b);
void decode(char mas[],long b);
void rev(int n);

void main()
{
char mas[MAX];
long p,b;

while(gets(mas))
{
b=strlen(mas);

if(!isdigit(mas[0]))
  p=0;
else if(isdigit(mas[0]))
  p=1;
if(!p)
 encode(mas,b);
else if(p)
 decode(mas,b);
}

}

void encode(char mas[],long b)
{
long a,n;
for(a=b-1;a>=0;a--)
 {

 n=mas[a];
 rev(n);
 
 }
printf("\n");
}

void decode(char mas[],long b)
{
char ch;
long marker,x,y,z,a,nn,de[MAX];

for(a=0;a<b;a++)
  de[a]=mas[a]-48;

marker=1;
for(;;)
{

x=b-marker;
marker++;
y=b-marker;
nn=10*de[x]+de[y];
if(nn>=32)
    {
    ch=(char)nn;
    printf("%c",ch);
    if(marker<b)
     marker++;
    }

else if(nn<32)
  {
  marker++;
  z=b-marker;
  nn=100*de[x]+10*de[y]+de[z];
  ch=(char)nn;
  printf("%c",ch);
  if(marker<b)
  marker++;
  }
if(marker==b) break;
}
printf("\n");
}
void rev(int n)
{
int a,b,c,d[10],e,f;


 for(c=0;;c++)
  {
  a=n%10;
  d[c]=a;
  b=n/10;
  if(b>=10)
   {
   n=b;
   }
  else
   {
   e=c+1;
   d[e]=b;
   break;
   }

  }
 for(f=0;f<=e;f++)
  printf("%ld",d[f]);

}

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

444

Post by Eric »

Why I get wrong answer? :-?
Last edited by Eric on Tue Oct 29, 2002 11:33 am, edited 1 time in total.

Robbie
New poster
Posts: 15
Joined: Wed Aug 07, 2002 11:38 am
Location: Viet Nam

Re: 444

Post by Robbie »

Try this :
input :
abc

798999
:wink:

GGG

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric »

inputs:
  • abc
    798999
    GGG
outputs:
  • 998979
    cba
    171717
Isn't it?

Robbie
New poster
Posts: 15
Joined: Wed Aug 07, 2002 11:38 am
Location: Viet Nam

Post by Robbie »

Eric wrote:inputs:
  • abc
    798999
    GGG
outputs:
  • 998979
    cba
    171717
Isn't it?
Hm..., my input isn't like that.
I want to tell you that there may be some spaces like this :

Code: Select all

    
input :
abc

       798999      

cba 
Good luck !

GGG :P

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Still WA

Post by Eric »

Thanks for your great help.
However, I go on to get wrong answer.
Still don't know where the problem is. :(
Last edited by Eric on Tue Oct 29, 2002 11:32 am, edited 1 time in total.

Robbie
New poster
Posts: 15
Joined: Wed Aug 07, 2002 11:38 am
Location: Viet Nam

Re: Still WA

Post by Robbie »

How about this :

Code: Select all

input
abc
    abc
      (blank line )
    798999

output
998979
99897923232323

cba 
701997118012300111111117
3333332351100101110150141120123101411792310178
GGG :D

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Accepted

Post by Eric »

Thanks a lot.
I finally manage to tackle the bugs in my program and get accpeted.
This is a really interesting problem. :D

knightmare
New poster
Posts: 4
Joined: Sat Nov 23, 2002 2:00 pm

Post by knightmare »

LOL!

I had a bug with the d character (100 in ascii, the bug was that it eliminated the 00 with atoi) and I only noticed it when I tried to decode the encrypted messages Robbie put in the end of is message (it gave me "Goo luck" instead of "Good luck")

Thnx ;)

Moinul(AUST)
New poster
Posts: 11
Joined: Tue Dec 10, 2002 11:40 am
Location: Dhaka, Bangaldesh
Contact:

No more WA !!

Post by Moinul(AUST) »

Many Thanks to Robbie,

Your last two line

701997118012300111111117
3333332351100101110150141120123101411792310178

helped me identifying a minor mistake in my program :lol:

-Moinul

b3yours3lf
New poster
Posts: 44
Joined: Wed Aug 14, 2002 3:02 am

Post by b3yours3lf »

I have take a look at your code and I find two problem
1. the message is at most 80 character long but if it is encode message so the character can be more than 80 character long (max 3* 80=240) so enlarge the array
2.
[c]for(f=0;f<=e;f++)
printf("%ld",d[f]); [/c]
the type if d is array of int but you use long int format to print it

razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

Thanx a lot!!!

Post by razibcse »

hi, thanx a lot for ur nice help..

I got it accepted...

I m really grateful to u coz u have given ur so precious time to look my code...

wish u good luck..
Razib

neowarez
New poster
Posts: 14
Joined: Tue May 06, 2003 11:04 pm
Location: Portugal
Contact:

WA?? Why??

Post by neowarez »

I have the same problem I dont no why but my source give me WA...

Why??

[pascal]

Program Acm_444(Input, Output);
Var
Letra: Char;
Dado: Integer;
Result: AnsiString;

Function Reverse(X: Integer): String;
Var
Y, Temp: String;
I, Outro: Integer;
Begin
Str(X, Y);
Temp:='';
For I:=Length(Y) DownTo 1 Do
Temp:=Temp+Copy(Y, I, 1);
Reverse:=Temp
End;

Procedure Reverse2(X: String);
Var
Temp: String;
I, Outro: Integer;
Begin
Temp:='';
For I:=Length(X) DownTo 1 Do
Temp:=Temp+Copy(X, I, 1);
Val(Temp, Dado, Outro);
End;

Procedure Encode;
Var
I, X: Integer;
Begin
Result:='';
If Not (Ord(Letra)=13) Then
Result:=Reverse(Ord(Letra));
If Not EoLn(Input) Then
Repeat
Read(Input, Letra);
If Not (Ord(Letra)=10) Then
Result:=Reverse(Ord(Letra))+Result;
Until EoLn(Input);
WriteLn(Output, Result);
End;

Procedure Decode;
Var
Letras: String;
Letra2: String[2];
Outro: Integer;
Begin
Result:='';
Letras:='';
Letras:=Letras+Letra;
Read(Input, Letra);
Letras:=Letras+Letra;
Reverse2(Letras);
If Not ((Upcase(Chr(Dado)) In ['A'..'Z']) Or (Dado In [32..63])) Then
Begin
Read(Input, Letra);
Letras:=Letras+Letra;
Reverse2(Letras);
End;
Result:=Chr(Dado)+Result;
If Not EoLn(Input) Then
Repeat
Letras:='';
Read(Input, Letra2);
Letras:=Letras+Letra2;
Reverse2(Letras);
If Not ((Upcase(Chr(Dado)) In ['A'..'Z']) Or (Dado In [32..63])) Then
Begin
Read(Input, Letra);
Letras:=Letras+Letra;
Reverse2(Letras);
End;
Result:=Chr(Dado)+Result;
Until EoLn(Input);
WriteLn(Output, Result);
End;

Procedure GetData;
Begin
Repeat
Read(Input, Letra);
If Ord(Letra) In [48..57] Then
Decode
Else
Encode;
ReadLn(Input);
Until EoF(Input);
End;

BEGIN
GetData;
END.
[/pascal]

Thanks.. :-? :-?
Neo

Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

444 - Encoder and Decoder

Post by Zhao Le »

I don't understand, I have tested a lot.

But still got WA.

Any anyone help me or give some test code.

Thanks in advance.

The code is deleted.
Last edited by Zhao Le on Sat Aug 02, 2003 6:52 am, edited 1 time in total.
AC makes me feels good,
But WA makes me thinks hard.

Culter
New poster
Posts: 2
Joined: Mon Jul 28, 2003 3:21 pm
Location: Zagreb

RE:Why #444 WA?

Post by Culter »

Try input:

Code: Select all

32
My ACC program generates:

Code: Select all

A
and yours:

Code: Select all

W
but I haven't found mistake in your code.

Post Reply

Return to “Volume 4 (400-499)”