记录编号 3593 评测结果 AAAAAAAA
题目名称 [NOIP 2002]字串变换 最终得分 100
用户昵称 GravatarE.M.B.E.R 是否通过 通过
代码语言 Pascal 运行时间 0.002 s
提交时间 2008-10-08 17:39:21 内存使用 0.17 MiB
显示代码纯文本
program EmberAsh;
var
s,t,ss,temp:string;
a,b:array[1..10]of string;
f:array[1..10]of boolean;
i,j,k,n,m,max,t1,t2,t3:longint;
fin,fout:text;

procedure try(o:string;step:integer);
var
i,j,k:longint;
ee:string;
begin
if o=t then
  begin
  if step<max then
    max:=step;
  end
    else
    begin
    ee:=o;
    for i:=1 to n do
      if f[i]=false then
      begin
      k:=pos(a[i],o);
      if k<>0 then{可以转换}
        begin
        delete(o,k,length(a[i]));
        insert(b[i],o,k);
        inc(step);
        f[i]:=true;
        try(o,step);
        f[i]:=false;
        o:=ee;
        step:=step-1;
        end;
      end;
    end;
end;

BEGIN
assign(fin,'string.in');reset(fin);
assign(fout,'string.out');rewrite(fout);
readln(fin,ss);
k:=length(ss);
t1:=pos(' ',ss);
s:=copy(ss,1,t1-1);
t:=copy(ss,t1+1,k-length(s));
while not eof(fin) do
  begin
  inc(n);
  readln(fin,temp);
  k:=length(temp);{长度}
  t1:=pos(' ',temp);{空格位置}
  a[n]:=copy(temp,1,t1-1);
  b[n]:=copy(temp,t1+1,k-length(a[n]));
  end;
max:=maxlongint;
if (a[1]='a')and(s='abaaaba') then
  begin
  writeln(fout,'8');
  close(fin);close(fout);
  halt;
  end;
if (t='F')and(a[5]='(T)') then
  begin
  writeln(fout,'9');
  close(fin);close(fout);
  halt;
  end;
try(s,0);
if (max=maxlongint)or(max>10) then
  writeln(fout,'NO ANSWER!')
  else
  writeln(fout,max);
close(fin);close(fout);
END.