记录编号 14104 评测结果 AAAAWAAA
题目名称 [NOIP 2002]字串变换 最终得分 87
用户昵称 Gravatarbing 是否通过 未通过
代码语言 Pascal 运行时间 0.173 s
提交时间 2009-10-26 21:22:49 内存使用 0.61 MiB
显示代码纯文本
program bing;
type
 sb=record
 d:integer;
 s:string;
end;
var
 f1,f2:text;
 a:array[0..1,1..1000] of sb;
 b:array[1..6,1..2] of string;
 t,h:array[0..1] of integer;
 n:integer;
 f:boolean;
procedure init;
var
 s:string; i:integer;
begin
 assign(f1,'string.in');reset(f1);
 assign(f2,'string.out');rewrite(f2);
 readln(F1,s);
 a[0,1].s:=copy(s,1,pos(' ',s)-1);
 a[1,1].s:=copy(s,pos(' ',s)+1,length(s)-pos(' ',s));
 a[0,1].d:=0;a[1,1].d:=0;
 i:=0;
 while not eof(f1) do
 begin
  readln(f1,s);
  inc(i);
  b[i,1]:=copy(s,1,pos(' ',s)-1);
  b[i,2]:=copy(s,pos(' ',s)+1,length(s)-pos(' ',s));
 end;
 n:=i;
 h[0]:=0;h[1]:=0;t[0]:=1;t[1]:=1;
end;

procedure ch(i:integer);
var
 k:integer;
begin
 f:=true;
 for k:=1 to t[i]-1 do
 begin
 if a[i,k].s=a[i,t[i]].s then f:=false;
 if not f then begin dec(t[i]);exit;end;
 end;
end;
procedure pc(i:integer);
var
 k:integer;
begin
 for k:=1 to t[1-i] do
 if a[1-i,k].s=a[i,t[i]].s then
 begin
 if a[1-i,k].d+a[i,t[i]].d-1<=10 then writeln(f2,a[1-i,k].d+a[i,t[i]].d)
 else writeln(f2,'NO ANSWER!');
 close(f1);close(f2);halt;
 end;
end;
procedure ex(i:integer);
var
 k:integer;s:string;dt:integer;
begin
 inc(h[i]);
 dt:=a[i,h[i]].d;
 for k:=1 to n do
 begin
  s:=a[i,h[i]].s;
  if (i=0)and(pos(b[k,1],s)<>0)or(i=1)and(pos(b[k,2],s)<>0) then
  begin
   inc(t[i]);
   if i=0 then
   begin
   s:=copy(s,1,pos(b[k,1],s)-1)+b[k,2]+copy(s,pos(b[k,1],s)+length(b[k,1]),length(s)-pos(b[k,1],s)-length(b[k,1])+1);
   a[i,t[i]].s:=s;
   end
   else
   begin
   s:=copy(s,1,pos(b[k,2],s)-1)+b[k,1]+copy(s,pos(b[k,2],s)+length(b[k,2]),length(s)-pos(b[k,2],s)-length(b[k,2])+1);
   a[i,t[i]].s:=s;
   end;
   a[i,t[i]].d:=dt+1;
   ch(i);
     if f then pc(i);
  end;
 end;
end;
procedure ss;
begin
 repeat
  if (t[0]<=t[1])and(t[0]>=h[0]) then ex(0);
  if (t[1]<=t[0])and(t[1]>=h[1]) then ex(1);
  if t[0]>=h[0] then ex(0);
  if t[1]>=h[1] then ex(1);
 until (h[0]>t[0])and(h[1]>t[1]);
 if (h[0]>t[0])and(h[1]>t[1]) then writeln(f2,'NO ANSWER!');
end;
begin
 init;
 ss;
 close(f1);close(f2);
end.