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

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

444

Post by Zhao Le » Tue Jul 29, 2003 3:56 am

Input is define here:
"A" 65 "a" 97 " " 32
"B" 66 "b" 98 "!" 33
. . "," 44
. . "." 46
. . ":" 58
"Y" 89 "y" 121 ";" 59
"Z" 90 "z" 122 "?" 63
if the ASCII value is 123, the encoded message should contain the string "321".
So I think the number you enter
32
is not valid.

It should be 23 or somthing like that.

And I finally got AC because the Matrix is too small when I turn it to 1000
got AC.
AC makes me feels good,
But WA makes me thinks hard.

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

problems 444 and 445 why PE

Post by chunyi81 » Tue Jul 29, 2003 5:23 pm

I actually solved these 2 problems, the online judge judged my programs to be accepted(PE). I have tested many inputs for my programs and the output is ok - no trailing spaces, missing/extra blank lines. So why is there a PE? I use Windows - MS DOS prompt to compile and run the programs. Could the problem be due to the statements System.out.print() and System.out.println() used together? I have read in another thread in this forum that System.out.println() prints a \r\n in Windows but \n in Linux, something like that. Could this have caused the PE?

Code for 444:
[java]
ACed
[/java]

and 445:

[java]
ACed
[/java]
Last edited by chunyi81 on Sun Jan 07, 2007 10:25 am, edited 1 time in total.

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry » Sat Aug 09, 2003 3:03 pm

Can someone post some input/output? Thanks

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Sat Aug 09, 2003 4:45 pm

You can create your own inputs/outputs.. assuming your encoding is correct since that part is rather trivial. Just use that output as your input for decoding.. =)

But, if you must insist, here's some random test data:

Input:
? ,,V!H.:!;
,HdCqgiA
e!ER!o hI,X?.?; S?SyHv. ,N,Q
::zr
?M?aEHQdbgC,R;umOI?jv?!M F
r!:Y?.!Fx?;rm;PS
JX, ::;SdY.,FM; G:.,?C
s ..EG!XLDm,H!bk
?wwQ!:Im w,F.F WOsbGja;NN
LA;hr!mdsUQ,!zd; :zrD?
Output:
9533856427336844442336
56501301311760012744
184487442364811271213836382395366436884437401232311133289633101
4112218585
0723773336811601363797901711952844763018900118279679367736
38089590141195360210733643698853341123
763644648517239577074464980013895858523448847
7018933274490186678833179664642323511
2387879579601178951197782307640744911239013785331891191136
36864112218523950012213344185851100190133411401955667
And vice versa.

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry » Sun Aug 10, 2003 4:44 am

Thanks... my output looks good on stdout, but when I stream it to a file, it turns out I output a null character between things by accident, because I did something like:

for ( i = strlen( b ); i >= 0; i-- )

when I should've done:

for ( i = strlen( b ) - 1; i >= 0; i-- )

ah well.. thanks!

Amir Faisal
New poster
Posts: 4
Joined: Wed Aug 13, 2003 2:47 pm
Location: Bangladesh

444-WA

Post by Amir Faisal » Sat Nov 15, 2003 10:01 am

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

void main()
{
char input[10000],temp[200000],t[3];
int l,i,j,num,k,m,count;

while(gets(input))
{

if(isdigit(input[0]))
{

l = strlen(input)-1;

for(i=0;i<l+1;i++)
temp = input[l-i];

temp = 0;

for(i=0;i<strlen(temp);)
{
num = 0,count = 0;

j=1;
if(temp=='1')
{
while(count!=3)
{
num += (temp-48)*pow(10,j);
j--;
count++;
i++;
}
}
else
{
while(count!=2)
{
num += (temp-48)*pow(10,j);
j--;
count++;
i++;
}
}


printf("%c", num);
}
}
else

{
strcpy(temp,"");
strcpy(t,"");
k = 0;

for(i=0;i<strlen(input);i++)
{
num = (int)input;

j = 0;

while(num)
{
t[j++] = num%10 + 48;
num = num/10;
}

t[j]=0;

l = strlen(t)-1;

for(m=0;m<l+1;m++)
{
temp[k++] = t[l-m];
}

temp[k] = 0;

}

for(i=strlen(temp)-1;i>=0;i--)
printf("%c", temp);


}

printf("\n");
}
}
I love my mother.

osan
New poster
Posts: 47
Joined: Tue Jul 29, 2003 12:03 pm
Location: Bangladesh,Dhaka.
Contact:

check this input & output

Post by osan » Wed Nov 19, 2003 5:11 pm

INPUT
abc
798999
Have a Nice Day !
9900199
abbadc
aaa
11167001
ABC
AbCD
OUTPUT
998979
cba
332312179862310199501872379231018117927
cdc
9900179898979
797979
dLo
766656
86768956
this time WA
what next...............?

soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

Post by soyoja » Fri Apr 16, 2004 4:29 am

Maybe the only trap of this problem is ASCII code values could be 2 or 3 digits. So If someone can solve this problem, he must check the code carefully.

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Fri Jun 11, 2004 6:13 am

Why do I get WA??? The code looks to work correct for all inputs I could find..[cpp]#include <iostream>
#include <map>
#include <string>
using namespace std;

string s, ans, now;
map < string, char > data;

string encode(char ch){
string out = "";
while(ch){
out = out + ((char)(ch%10 + '0'));
ch /= 10;
}
return out;
}

int main(){
data.clear();
for(char i = 'a'; i <= 'z'; i++) data[encode(i)] = i;
for(char i = 'A'; i <= 'Z'; i++) data[encode(i)] = i;
data[encode('!')] = '!';
data[encode(',')] = ',';
data[encode('.')] = '.';
data[encode(':')] = ':';
data[encode(';')] = ';';
data[encode('?')] = '?';
data[encode(' ')] = ' ';

while(getline(cin, s)){
if(s[0] >= '0' && s[0] <= '9'){
now = "";
ans = "";
for(unsigned long i = 0; i < data.size(); i++){
now = now + s;
if(data.find(now) != data.end()){
ans = data[now] + ans;
now = "";
}
}
cout << ans;
} else{
for(long i = s.size() - 1; i >= 0; i--)
cout << encode(s);
}
cout << endl;
}


return 0;
}
[/cpp]

Dani Rodrigo
New poster
Posts: 11
Joined: Sun Jul 18, 2004 1:39 am

Post by Dani Rodrigo » Wed Aug 18, 2004 1:06 am

Be carefully when if you declare an array to save the numbers. The size of this array must be at least 300!! (because there are a maximum of 100 characters).
I made this error and get WA to many times. :D

Bye.

oulongbin
Learning poster
Posts: 53
Joined: Sat Jul 10, 2004 5:57 pm
Location: Shanghai China

444 WA

Post by oulongbin » Wed Aug 25, 2004 4:48 am

I think the code doens't have any bug. What's wrong with the code?
[cpp]
#include <iostream>
using namespace std;
#include <cmath>
#include <cstdio>
int main()
{
char a[200];
char c[300];
int t;
int len;
int i,j;
while(cin.getline(a,200))
{
if(a[0]>='0'&&a[0]<='9')
{
len=strlen(a);
j=0;
for(i=len-1;i>=0;)
{
if(a=='1'&&i-1>=0&&i-2>=0)
{
c[j++]=char((int(a)-48)*100+(int(a[i-1])-48)*10+int(a[i-2])-48);
i-=3;
}
else
{
c[j++]=char((int(a)-48)*10+int(a[i-1]-48));
i-=2;
}
}
for(i=0;i<j;i++)
{
cout<<c;
}
cout<<endl;
}
else
{
len=strlen(a);
t=0;
j=0;
for(i=len-1;i>=0;i--)
{
t=int(a);
while(t!=0)
{
c[j++]=char(t%10+48);
t/=10;
}
}
for(i=0;i<j;i++)
{
cout<<c;
}
cout<<endl;
}
}
return 0;
}
[/cpp]

User avatar
J&Jewel
New poster
Posts: 50
Joined: Thu Jul 31, 2003 10:43 am
Location: Daffodil University,Dhaka,Bangladesh
Contact:

444 encode decode

Post by J&Jewel » Mon Jan 03, 2005 12:05 pm

Please help me i got w/A why..?
I hate Wrong Answer!

User avatar
J&Jewel
New poster
Posts: 50
Joined: Thu Jul 31, 2003 10:43 am
Location: Daffodil University,Dhaka,Bangladesh
Contact:

444 encode decode

Post by J&Jewel » Mon Jan 03, 2005 12:08 pm

Please help me i got w/A why..?
#include<stdio.h>
#include<string.h>
char input[81];
int valid[128];
int init()
{
int i;
for(i=0;i<128;i++)
valid=0;
for(i=65;i<=90;i++)
valid=1;
for(i=97;i<=122;i++)
valid=1;
valid[32]=1;valid[33]=1;valid[44]=1;valid[46]=1;
valid[58]=1;valid[59]=1;valid[63]=1;
return 0;
}
int rev(int len)
{
char temp;
int len1=len;
for(int i=0;i<len/2;i++)
{
temp=input[len1-1];
input[len1-1]=input;
input=temp;
len1--;
}
return 0;
}
int main()
{
int i,r,len,ch,t,j;
init();
while(gets(input)!=NULL)
{
len= strlen(input);
rev(len);
t=0; j=0;
for(i=0;i<len;)
{
if((input>='A'&& input<='Z')||(input>='a'&&input<='z')||input==' '||input[i]=='!'||input[i]==','||input[i]=='.'||input[i]==':'||input[i]==';'||input[i]=='?')
{
ch=input[i];
while(ch>=1)
{
r = ch%10;
printf("%d",r);
ch = ch/10;
}
i++;
}
else
{
while(j<3 && valid[t]!=1)
{
t=t*10;
t=t+(input[i]-'0');
j++;
i++;
}
printf("%c",t);
}
t=0;j=0;
}

printf("\n");
}
return 0;
}
I hate Wrong Answer!

User avatar
J&Jewel
New poster
Posts: 50
Joined: Thu Jul 31, 2003 10:43 am
Location: Daffodil University,Dhaka,Bangladesh
Contact:

444 encode decode

Post by J&Jewel » Mon Jan 03, 2005 12:08 pm

Please help me i got w/A why..?
#include<stdio.h>
#include<string.h>
char input[81];
int valid[128];
int init()
{
int i;
for(i=0;i<128;i++)
valid=0;
for(i=65;i<=90;i++)
valid=1;
for(i=97;i<=122;i++)
valid=1;
valid[32]=1;valid[33]=1;valid[44]=1;valid[46]=1;
valid[58]=1;valid[59]=1;valid[63]=1;
return 0;
}
int rev(int len)
{
char temp;
int len1=len;
for(int i=0;i<len/2;i++)
{
temp=input[len1-1];
input[len1-1]=input;
input=temp;
len1--;
}
return 0;
}
int main()
{
int i,r,len,ch,t,j;
init();
while(gets(input)!=NULL)
{
len= strlen(input);
rev(len);
t=0; j=0;
for(i=0;i<len;)
{
if((input>='A'&& input<='Z')||(input>='a'&&input<='z')||input==' '||input[i]=='!'||input[i]==','||input[i]=='.'||input[i]==':'||input[i]==';'||input[i]=='?')
{
ch=input[i];
while(ch>=1)
{
r = ch%10;
printf("%d",r);
ch = ch/10;
}
i++;
}
else
{
while(j<3 && valid[t]!=1)
{
t=t*10;
t=t+(input[i]-'0');
j++;
i++;
}
printf("%c",t);
}
t=0;j=0;
}

printf("\n");
}
return 0;
}
I hate Wrong Answer!

gits
New poster
Posts: 19
Joined: Sun Oct 26, 2003 10:08 pm
Location: Aveiro, Portugal
Contact:

Post by gits » Sat Jan 08, 2005 6:04 pm

I had 2 problems compiling your program, first was this line

Code: Select all

for(int i=0;i<len/2;i++)
the declaration of i inside the loop isn't valid C89 (however, if this is C++ it's ok). Just declare i like the other variables.

The other was a warning about using gets, you shouldn't use that function; use fgets instead; but remember that fgets doesn't remove the last character if it is a newline (you can do that with str[strlen(str)-1] = '\0').

I tested your program with my sample inputs and the outputs were all correct, maybe you're getting a Compilation Error?

Post Reply

Return to “Volume 4 (400-499)”