不是VIP会员,不能显示答案

题目解答

题目:
var
left, right, father:array[1..20] of integer;
sl, s2, s3:string;
n,ana :integer;
procedure check(x:integer);
begin
if left[x]>0 then check(left[x));
s3 := s3 + sl[x];
if right[x]>0 then check(right[x]);
end;

procedure calc(x,dep :integer);
begin
ans:= ans + dep*(ord(sl[x])-ord('A')+1);
if left[x] > 0 then calc(left[x],dep+l);
if right[x]> 0 then calc(right[x),dep+l);
end;

procedure dfs(x,th :integer);
begin
if th = n+1 then
begin
s3 :='';
check(1);
if s2=s3 then
begin
ans := 0;
calc(1,1);
writeln(ans);
end;
exit;
end;
if (left[x]=0) and (right[x]=0) then
begin
left[x) := th;
father[th] := x;
dfs(th, th+1);
father[th] := 0;
left[x] := 0;
end;
if right[x] = 0 then
begin
right[x] := th;
father[th] := X;
dfs(th, th+1);
father[th] := 0;
right[x] := 0;
end;
if (father[x] > 0) then
dfs(father[x],th);
end;

begin
readln(s1);
readln(s2);
n := length(s1);
fillchar(left,sizeof(left),0);
fillchar(right,sizeof(right),0);
fillcahr(father,sizeof(father),0);
dfs(1,2);
end.

输入:
ABCDEF
BCAEDF
输出:55
考点: 0
分析:
解答: 由先序遍历、中序遍历的序列构造出二叉树,再求出节点对应值的和——ABCDEF基本值分别对应123456,求出的是各个节点基本值乘以该节点深度的和
评论:
老师: 0