[pascal]
Program P112_Tree_Summing;
Var
t : String;
Aim : Integer;
Ch : Char;
Procedure GetChar(var ch:Char);
Begin
Read(Ch);
While Not Eof(input) And
(Ch<>'(') And
(Ch<>')') And
(Ch<>'-') And
(Ch<>'+') And
((Ch>'9') Or
(Ch<'0')) Do Read(Ch);
End;
Function Find(Sum:Integer):Byte;
Var
x,y,Num : Integer;
Begin
GetChar(Ch);
If Ch=')' Then Begin
GetChar(Ch);
Find:=2;
Exit;
End;
t:='';
While Ch<>'(' Do
Begin
t:=t+Ch;
GetChar(Ch);
End;
Val(t,Num,Num);
Inc(Sum,Num);
x:=Find(Sum);
y:=Find(Sum);
If (x=0) Or (y=0) Or
((x=2) And (y=2) And (Sum=Aim))
Then Find:=0
Else Find:=1;
GetChar(Ch);
End;
Begin
GetChar(ch);
While Not Eof(Input) Do
Begin
t:='';
While Ch<>'(' Do
Begin
t:=t+Ch;
GetChar(Ch);
End;
If Eof(Input) Then Break;
Val(t,Aim,Aim);
If Find(0)=0
Then Writeln('yes')
Else Writeln('no');
End;
End.
[/pascal][/code]
"Your program has died with signal 11 (SIGSEGV). Meaning:
Invalid memory reference"
to my programme and I can't see way...
[pascal]
program p112(input,output);
type pnod=^nod;
nod=record
nr:integer;
st,dr:pnod;
end;
var rad:pnod;
s:string;
p,nr:integer;
procedure tree(var rad:pnod;var p:integer);
var n:integer;
begin
if s[p+1]=')' then
begin
rad:=NIL;
p:=p+1;
end
else
begin
n:=0;
p:=p+1;
while s[p] in ['0'..'9'] do
begin
n:=n*10+ord(s[p])-ord('0');
p:=p+1;
end;
new(rad);
rad^.nr:=n;
tree(rad^.st,p);
p:=p+1;
tree(rad^.dr,p);
p:=p+1;
end;
end;
function verif(s:integer; rad:pnod):boolean;
begin
if rad=NIL then
if s=0 then
verif:=true
else
verif:=false
else
verif:=verif(s-rad^.nr,rad^.st) or verif(s-rad^.nr,rad^.dr);
end;
procedure sterg(var s:string);
var i:integer;
begin
i:=pos(' ',s);
while i<>0 do
begin
delete(s,i,1);
i:=pos(' ',s);
end;
end;
procedure numara(s:string;var n1,n2:integer);
var i:integer;
begin
for i:=1 to length(s) do
if s='(' then
n1:=n1+1
else
if s=')' then
n2:=n2+1;
end;
procedure citire(var s:string; var nr:integer);
var i,cod:integer;
s1:string;
n1,n2:integer;
begin
readln(s);
while s[1]=' ' do
delete(s,1,1);
while s[length(s)]=' ' do
delete(s,length(s),1);
i:=pos(' ',s);
if i=0 then
begin
val(s,nr,cod);
s:='';
end
else
begin
s1:=copy(s,1,i-1);
val(s1,nr,cod);
delete(s,1,i);
sterg(s);
end;
n1:=0; n2:=0;
numara(s,n1,n2);
while (n1<>n2) or ((n1=n2) and (n1=0)) do
begin
readln(s1);
sterg(s1);
numara(s1,n1,n2);
insert(s1,s,length(s)+1);
end;
end;
procedure destroy(var rad:pnod);
begin
if rad<>NIL then
begin
destroy(rad^.st);
destroy(rad^.dr);
dispose(rad);
rad:=NIL
end;
end;
begin
while not eof(input) do
begin
citire(s,nr);
p:=1;
tree(rad,p);
if verif(nr,rad) then
writeln('yes')
else
writeln('no');
destroy(rad)
end;
end.
[/pascal]
Sorry I didn't take a close look on your programme. But, as far as I know, dividing by 0 or trying to manipulate the pointer which is assigned NIL would usually lead to Runtime Error. Try to enlarge your array or something like that.
function find(top:integer) : boolean;
var
root: integer;
begin
root:=0;
sign:=false;
inc(deep);
if (ch=')') then begin
find:=false;
read(ch);
end
else if (not ans) then begin
if (ch in ['-','+']) then begin
if ch='-' then sign:=true;
read(ch);
end;
while (ch in ['0'..'9']) do begin
root:=root*10+(ord(ch)-ord('0'));
read(ch);
end;
if sign then root:=root*-1;
while (ch<>'(') do read(ch);
read(ch);
a1:=find(root+top);
while (ch<>'(') do read(ch);
read(ch);
if not ans then a2:=find(root+top)
else a2:=true;
if (a1=a2) and (a1=false) and (root+top=num) then ans:=true;
if (not eoln) then read(ch);
find:=true;
end;
dec(deep);
end;
begin
while not eof do begin
read(num);
ans:=false;
read(ch);
while (ch=' ') do read(ch);
read(ch);
deep:=0;
find(0);
if ans then writeln('yes')
else writeln('no');
if ans and (not eoln) then readln;
end;
end.[/pascal]