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
输出:1
