记录编号 2983 评测结果 AAWAWAAT
题目名称 [NOIP 2002]字串变换 最终得分 62
用户昵称 Gravatarzpl123 是否通过 未通过
代码语言 Pascal 运行时间 1.127 s
提交时间 2008-09-29 22:45:47 内存使用 5.02 MiB
显示代码纯文本
program str;
const
max=10000;
var
m:integer;
s1,s2,first_s,end_s:string;
a,b:array[1..8]of string;
f:array[1..max]of string;
state:array[1..max]of record
                       s:string;
                       step:integer;
                       end;
procedure get(ss:string);
   var
    i,l:integer;
   begin
    s1:='';
    s2:='';
    l:=length(Ss);
    i:=pos(' ',ss);
    s1:=copy(ss,1,i-1);
    s2:=copy(ss,i+1,l-i);
   end;
procedure init;
  var
   s:string;
   i:integer;
  begin
    assign(input,'string.in');
    reset(input);
    assign(output,'string.out');
    rewrite(output);
    readln(s);
    get(s);
    first_s:=s1;
    end_s:=s2;
     i:=1;
    while not(eof) do
     begin
      readln(s);
      get(s);
      a[i]:=s1;
      b[i]:=s2;
      i:=i+1;
     end;
     m:=i-1;
    f[1]:=first_s;
    state[1].s:=first_s;
  end;
function ok(ss:string;p:integer):boolean;
  var
   ii:integer;
  begin
   ok:=true;
   for ii:=1 to p-1 do
    if f[ii]=ss then begin ok:=false;exit; end;
  end;

procedure main;
var
head,tail,o,t,l,l2,i,j:longint;
temp:string;
begin
head:=1;
tail:=1;
o:=1;
while (state[head].s<>end_s)and(head<=tail)and(state[head].step<=10) do
 begin
 for i:=1 to m do
  begin
  if (pos(a[i],state[head].s)<>0) then
   begin
   t:=pos(a[i],state[head].s);
   l:=length(a[i]);
   l2:=length(state[head].s);
   temp:='';
   for j:=1 to t-1 do
   temp:=temp+state[head].s[j];
   temp:=temp+b[i];
   for j:=t+l to l2 do
   temp:=temp+state[head].s[j];
   if ok(temp,o) then
    begin
    inc(o);
    f[o]:=temp;
    inc(tail);
    state[tail].s:=temp;
    state[tail].step:=state[head].step+1;
    end;{then2}
   end;{then1}
  end;{for i}
  inc(head);
 end;{while}
if (state[head].step<=10)and(state[head].s=end_s) then writeln(state[head].step)
 else writeln('No Answer!');
close(input);
close(output);
end;

begin
init;
main;
end.