458 - The 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

edmanm
New poster
Posts: 13
Joined: Sat Feb 09, 2002 2:00 am

Post by edmanm » Wed Feb 27, 2002 9:03 am

Can anybody give me some sample input to use to test my program for #458? My program works with the sample input given on the website, but I still get a W.A. What's the tricky input on this one??

pochmann
New poster
Posts: 28
Joined: Sat Jan 26, 2002 2:00 am
Contact:

Post by pochmann » Thu Feb 28, 2002 5:20 am

The input contains characters outside of ASCII range, that is, larger than 127. Maybe you use signed char and don't translate it when it's smaller than 32+7 or sth like that?

If that doesn't help, would you mind posting your solution?

Stefan

edmanm
New poster
Posts: 13
Joined: Sat Feb 09, 2002 2:00 am

Post by edmanm » Thu Feb 28, 2002 6:26 am

Here's my code so far. Am I on the right track?:

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

main()
{

char coded[3000];
char decoded[3000];
int length, i;
int key;

gets(coded);
key = coded[0] - '*';

do
{
length = strlen(coded);

for(i=0;i<length;i++)
decoded = coded - key;

for(i=0;i<length;i++)
printf("%c", decoded);

printf("n");
}while(gets(coded));

return 0;

}

pochmann
New poster
Posts: 28
Joined: Sat Jan 26, 2002 2:00 am
Contact:

Post by pochmann » Thu Feb 28, 2002 6:56 am

Yes you are. But the key is always 7. Maybe that's your problem. Wasn't stated in the description, very bad.

Stefan

edmanm
New poster
Posts: 13
Joined: Sat Feb 09, 2002 2:00 am

Post by edmanm » Thu Feb 28, 2002 7:29 am

That was all I had to change! I was under the impression that the key could be something other than 7, but I was wrong.

Thank you much pochmann!!

ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

ACM #458: What's wrong?

Post by ec3_limz » Fri Jun 28, 2002 1:24 pm

I keep getting wrong answer. What's wrong?

--------------------------------------------------------------------------------------
/* @JUDGE_ID: 18472 458 C++ */

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

int main() {
char line[256];
int len, i;

while (!feof(stdin)) {
gets(line);

len = strlen(line);
for (i = 0; i < len; i++)
printf("%c", static_cast<char>(line - 7));
printf("%c", '\n');
}

return 0;
}[/cpp]

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard » Fri Jun 28, 2002 1:39 pm

your end condition is false. feof() returns true only after one read function failed. in this the last line is repeated twice. solution:

Code: Select all

for (;;) {
  gets(line);
  if (feof(stdin))
    break
...
or simpler:

Code: Select all

while (gets(line)) {
...
[/code]

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

458 - help me with speed!

Post by liusu » Sat Aug 10, 2002 5:00 am

the problem 458:
my pascal code run 5.1 second ,but c++ code only 0.2 seconds,why?
who can help me?

:cry: :evil:
[pascal]var
str:string;
i:integer;
begin
while not eof do
begin
readln(str);
for i:=1 to length(str) do
write(chr(ord(str)-7));
writeln;

end;
end.[/pascal]

36495KN
New poster
Posts: 2
Joined: Thu Sep 25, 2003 6:42 am

Re: help me with speed!

Post by 36495KN » Thu Sep 25, 2003 6:46 am

me too!
and I consider why it use 324KBytes for the program?
:cry:

Your PASCAL program has solved Ok the problem 458 (The Decoder)
in 3.742 seconds using as much as 324 kbytes of virtual memory.
Congratulations

liusu wrote:the problem 458:
my pascal code run 5.1 second ,but c++ code only 0.2 seconds,why?
who can help me?

:cry: :evil:
[pascal]var
str:string;
i:integer;
begin
while not eof do
begin
readln(str);
for i:=1 to length(str) do
write(chr(ord(str)-7));
writeln;

end;
end.[/pascal]

36495KN
New poster
Posts: 2
Joined: Thu Sep 25, 2003 6:42 am

by this code, 0.338 seconds, 320Kbytes

Post by 36495KN » Thu Sep 25, 2003 7:50 am

[pascal]
var s1: string;
l1, i: integer;
ch1: char;

begin
while (not eof) do begin
readln(s1);
l1:= length(s1);

for i:=1 to l1 do begin
ch1:= s1;
ch1:= chr((ord(ch1)) - (ord('J')) + (ord('C')));
s1:= ch1;
end;
writeln(s1);
end;
end.
[/pascal]

:D

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Thu Sep 25, 2003 2:25 pm

Accepted (P.E.) in 0.070 sec:
[pascal]program p458;

var
line:ansistring;
buffer:ansistring;
bufptr:integer;
inptr:^char;
begin
setlength(buffer,1000000);
bufptr:=0;
while not eof do begin
readln(line);
inptr:=@line[1];
while(inptr^<>#0) do begin
inc(bufptr);
buffer[bufptr]:=chr(ord(inptr^)-7);
inc(inptr);
end;
inc(bufptr);
buffer[bufptr]:=#13;
inc(bufptr);
buffer[bufptr]:=#10;
end;
setlength(buffer,bufptr);
writeln(buffer);
end.[/pascal]

I'm not shure how to get rid of the P.E., but who cares...
Write and writeln are very slow compared to their C pendants, so to gain speed in an output intensive problem, you have to do your own buffering.

B.t.w. read and readln don't have that slowness: all the data for this problem is read in 0.027 sec using readln statements as above.

Notice that there are also 0.000 sec submissions in the ranking. These times are impossible, because you can't read and write all the judges data in this small amount of time. Obviously there are ways to fool the judge...

Jed Humphries
New poster
Posts: 1
Joined: Thu Mar 25, 2004 9:48 pm

458 - The Decoder

Post by Jed Humphries » Thu Mar 25, 2004 9:51 pm

I am a new poster and I have been trying to get this program accepted but I cant seem to make it accept it and I have read all the notes about Java. Can any of you java gurus help me out with this code:[java]
import java.io.*;
class Main
{
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}

catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof

return (new String (lin, 0, lg));
}

public static void main(String [] args) throws IOException
{
Main myWork = new Main(); // create a dinamic instance
//check to see if the next char is printable
while(((int)System.in.read()>=39)||((int) System.in.read()>=126))
myWork.Begin();
}
void Begin()
{
String input="", output="";
input = ReadLn(255);
input = input.substring(0,input.length()-1);
for (int i=0; i<input.length(); i++)
output = output + (char) (input.charAt(i)-7);

System.out.println(output);
}
}[/java]

Wane
New poster
Posts: 1
Joined: Fri Apr 02, 2004 9:16 pm

458 RunTimeError

Post by Wane » Fri Apr 02, 2004 9:25 pm

i've try if(get()==EOF)break;
but i didn't work
please help me

#include <stdio.h>


int main(){
char* sentence;

int i;
while(sentence[0]!='\0'){

gets(sentence);

for(i=0;sentence!='\0';i++){
sentence=sentence-7;
}
printf("%s\n",sentence);

}

return 0;
}

midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

O.L.E. in 458

Post by midra » Tue Apr 27, 2004 3:20 am

I get Output Limit Exceeded
WHY???

here is my code:
[c]#include <stdio.h>


int main()
{
char c[1000001];
int i=1,j; /*counters*/

while(scanf("%c",&c)!=NULL)
{
if (c=='\n')
{
for (j=1;j<=(i-1);j++)
printf("%c",c[j]-7);
printf("\n");
i=1;
}
i++;
}
return 0;
}[/c]

please help me!!!

Dejarik
New poster
Posts: 32
Joined: Sun Mar 07, 2004 1:23 pm
Location: Barcelona, SPAIN
Contact:

Post by Dejarik » Tue Apr 27, 2004 9:27 am

Maybe replacing

Code: Select all

while(scanf("%c",&c[i])!=NULL)
with

Code: Select all

while(scanf("%c",&c[i])!=EOF)
will work?

Hope it helps...

PD: Why do you need an array to store chars if you manage them individually with scanf((char))? Try to store each char readed in a simple char variable and then print it with -7 ascii code. It should also works saving a lot of memory space, isn't it?

Joan

Post Reply

Return to “Volume 4 (400-499)”