458 - The Decoder
Moderator: Board moderators
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;
}
#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;
}
ACM #458: What's wrong?
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]
--------------------------------------------------------------------------------------
/* @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]
your end condition is false. feof() returns true only after one read function failed. in this the last line is repeated twice. solution:
or simpler:
[/code]
Code: Select all
for (;;) {
gets(line);
if (feof(stdin))
break
...
Code: Select all
while (gets(line)) {
...
458 - help me with speed!
the problem 458:
my pascal code run 5.1 second ,but c++ code only 0.2 seconds,why?
who can help me?
[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]
my pascal code run 5.1 second ,but c++ code only 0.2 seconds,why?
who can help me?


[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]
Re: help me with speed!
me too!
and I consider why it use 324KBytes for the program?
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
and I consider why it use 324KBytes for the program?

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?
![]()
![]()
[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]
by this code, 0.338 seconds, 320Kbytes
[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]

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]

- little joey
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 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...
[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...
-
- New poster
- Posts: 1
- Joined: Thu Mar 25, 2004 9:48 pm
458 - The Decoder
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]
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]
458 RunTimeError
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;
}
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;
}
O.L.E. in 458
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!!!
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!!!
Maybe replacing
with
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
Code: Select all
while(scanf("%c",&c[i])!=NULL)
Code: Select all
while(scanf("%c",&c[i])!=EOF)
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