10340 - All in All

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

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

10340 - All in All

Post by Dominik Michniewski » Thu Aug 01, 2002 10:17 am

What's wrong with this code ?? I got WA, but I think, that it could be correct ... maybe any trap I'm missing ?? PLease help

Code: Select all

CUT
I found, that I can edit previously posted messages ;-) so I cut source code ... I'm not sure, that is correct way to prevent someone to copy-paste method to solve problems ... :-( but I think, that only when source code has errors should be post here...
Last edited by Dominik Michniewski on Wed Sep 11, 2002 8:20 am, edited 1 time in total.

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » Thu Aug 01, 2002 11:42 am

Change this:
If (len1>len2) ...
and after
if (*p == 0) return 0;
there should be p++;
because you can't use the same letter twice.

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

Post by Dominik Michniewski » Thu Aug 01, 2002 12:46 pm

Thanks, thanks and again thanks Adrian ....
I forgot about incrementing p after while loop :oops:
I got Accepted now :-))

Dominik
BTW. In description I can't see where it written that the same strings are for "Yes" answer ... but OK :-)) Thanks again :-)

Rivaldo
New poster
Posts: 19
Joined: Sun Jul 07, 2002 3:59 pm

10340

Post by Rivaldo » Sun Aug 04, 2002 11:53 am

I think my program is no problem. But it got runtime error.

[pascal]
const
name1 = '';
name2 = '';
maxn = 10000;
var
s,t : array[1 .. maxn] of char;
ls,lt : integer;

procedure init;
var w : char;
begin
ls := 0; lt := 0;
fillchar(s, sizeof(s), 0);
fillchar(t, sizeof(t), 0);
read(w);
while w <> ' ' do begin
inc(ls);
s[ls] := w;
read(w);
end;
while w = ' ' do read(w);
while (w <> ' ') and not eoln do begin
inc(lt);
t[lt] := w;
read(w);
end;
if w <> ' ' then begin inc(lt); t[lt] := w; end;
readln;
end;

procedure main;
var i,j : integer;
begin
j := 1;
for i := 1 to ls do begin
while (j <= lt) and (s <> t[j]) do inc(j);
if s <> t[j] then begin
writeln('No'); exit;
end
else if i = ls then
if i = ls then writeln('Yes')
else
else inc(j);
end;
end;

begin
assign(input, name1);
reset(input);
assign(output, name2);
rewrite(output);

while not eof do begin
init;
main;
end;
close(input);
close(output);
end.
[/pascal]

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

Post by xenon » Sun Aug 04, 2002 12:37 pm

Why open another tread for this problem?

Runtime Errors give information about the kind of error that was encountered, so look at that information to solve the problem. In your case you probably got 'Illegal Memory Reference', or something like that, which means that the program tries to access memory that doesn't belong to it.
Since you only allocated 10000 bytes of memory for the two inputbuffers, they are the most probable offenders. Try to increase their sizes.

Rivaldo
New poster
Posts: 19
Joined: Sun Jul 07, 2002 3:59 pm

Post by Rivaldo » Sun Aug 04, 2002 3:34 pm

Thanks! I got AC now. :D

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

10340

Post by htl » Wed Aug 21, 2002 1:13 pm

Why does this code get RE? Is it possible that the length of
the string over 10000?
[c]
#include<stdio.h>
#include<string.h>
void main(void)
{
int **table,x,lens,lent,y,p;
char *s,*t;
s=(char *)malloc(sizeof(char)*10001);
t=(char *)malloc(sizeof(char)*10001);
table=(int **)malloc(sizeof(int *)*2);
for(x=0;x<2;x++)
table[x]=(int *)malloc(sizeof(int)*10001);
while(scanf("%s %s",s,t)!=EOF)
{
lens=strlen(s);
lent=strlen(t);
for(x=0;x<=lent;x++)
table[0][x]=0;
for(x=0,p=1;x<lens;x++,p=1-p)
{
table[p][0]=0;
for(y=0;y<lent;y++)
if(s[x]==t[y])
table[p][y+1]=table[1-p][y]+1;
else if(table[p][y]>=table[1-p][y+1])
table[p][y+1]=table[p][y];
else
table[p][y+1]=table[1-p][y+1];
}
if(table[1-p][lent]==lens)
printf("Yes\n");
else
printf("No\n");
}
free(table);
free(s);
free(t);
}
[/c]

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » Wed Aug 21, 2002 2:24 pm

Yes, it is possible, why do you ask? It is obvious that this would cause a runtime error, so if you get RE in a problem like this you should first try to allocate more memory before asking in the forum, and please use existing threads (there is one about this problem). In this case the string is not longer than 500000 and perhaps shorter, but if you want to be sure use 500000 as upper limit.

HKcow
New poster
Posts: 10
Joined: Fri Aug 30, 2002 2:34 pm

10340, why wa?

Post by HKcow » Fri Aug 30, 2002 2:37 pm

Could any one help me to check why it is getting wa?

[cpp]
#include <stdio.h>
#include <string.h>

void main() {
char *a, *b;
a = new char [200000];
b = new char [200000];

int i, j;
while (scanf("%s %s", a, b)== 2) {
j=0;
for (i=0; (a!='\0') && (b[j]!='\0'); i++)
for (; (b[j]!='\0') && (a!=b[j]); j++);
if (b[j]!='\0')
printf("Yes\n");
else
printf("No\n");
}
delete [] a;
delete [] b;
}[/cpp]

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

Post by Dominik Michniewski » Fri Aug 30, 2002 3:46 pm

try with
aaa aaa

answer is "Yes" - you have "No" (I think)
I made same mistake ...

HKcow
New poster
Posts: 10
Joined: Fri Aug 30, 2002 2:34 pm

Post by HKcow » Fri Aug 30, 2002 8:35 pm

Oh thx, but I got this right.
But
aa bab
i will get correct~
^_^, thx
HKcow

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

Post by Eric » Sun Oct 27, 2002 6:07 pm

Surely, your array is too small :D

jamesng
New poster
Posts: 3
Joined: Sun Aug 18, 2002 5:33 pm
Location: Singapore
Contact:

Post by jamesng » Sat Nov 23, 2002 5:47 am

Hi Homeway,

I too got WA for my program. May I know what you discovered about your program that's wrong? Thanx alot.

zsepi
Learning poster
Posts: 51
Joined: Thu Sep 26, 2002 7:43 pm
Location: Easton, PA, USA

10340 - about to give up

Post by zsepi » Fri Dec 27, 2002 1:42 am

if someone could explain me why I get WA on this, it would be appreciated... I tested it with everything I could think of and it worked fine....
here is the code
[cpp]
#include <iostream.h>
#include <string.h>

#define MX 1000000

main() {
char o[MX],e[MX]; /* original, encoded */
int i,j,ol,el;

cin >> o >> e;
while(!cin.eof()) {
i=0;
j=0;
ol=strlen(o);
el=strlen(e);
if(ol<=el) {
while(i<ol) {
while(j<el && o!=e[j]) j++;
if(j==el) break;
i++;
}
if(o==0) cout << "Yes" << endl;
else cout << "No" << endl;
}
else cout << "No" << endl;
cin >> o >> e;
}
}
[/cpp]
thanks!

tat tvam asi
New poster
Posts: 30
Joined: Sat Nov 30, 2002 1:04 pm

Post by tat tvam asi » Mon Dec 30, 2002 11:36 pm

helo
... try the following
input "file" :
A A~
where ~ stands for EOF .
bye

Post Reply

Return to “Volume 103 (10300-10399)”