NOIp2002 T2 字串变换

题目要求最小变换次数,从搜索的角度讲,可以用BFS或者迭代加深。第一次写双向BFS,感觉还算标准的。判重的时候直接暴力O(tail)For循环了,不过依然很快~

Const
        maxn=10000;
        maxq=100000;
Var
        a:array[0..1,0..maxn]of string;
        q:array[0..1,0..maxq]of string;
        step:array[0..1,0..maxn]of longint;
        head,tail:array[0..1]of longint;
        int,aim,s1,s2,s:string;
        n:longint;
Procedure split(s:string);
 var
        k:longint;
 begin
        k:=pos(' ',s);
        s1:=copy(s,1,k-1);
        s2:=copy(s,k+1,length(s)-k);
 end;
Procedure init;
 begin
        readln(s);
        split(s);
        int:=s1;aim:=s2;
        n:=0;
        while not eof do
                begin
                        readln(s);
                        if s='' then exit;
                        inc(n);
                        split(s);
                        a[0,n]:=s1;a[1,n]:=s2;
                end;
 end;
Function vis(s:string;t:byte):boolean;
 var
        i:longint;
 begin
        vis:=false;
        for i:=1 to tail[t] do if q[t,i]=s then exit(true);
 end;
Procedure print(k:longint);
 begin
        writeln(k);
        halt;
 end;
Procedure check(t:byte);
 var
        i:longint;
 begin
        for i:=1 to tail[1-t] do
                if q[1-t,i]=q[t,tail[t]] then print(step[1-t,i]+step[t,tail[t]]);
 end;
Procedure bfs(t:byte);
 var
        i,j,k:longint;
        pre,tmp:string;
 begin
        inc(head[t]);pre:=q[t,head[t]];
        for i:=1 to n do
                begin
                k:=length(a[t,i]);
                for j:=1 to length(pre)-k+1 do
                        begin
                                if copy(pre,j,k)=a[t,i] then
                                        begin
                                                tmp:=copy(pre,1,j-1)+a[1-t,i]+copy(pre,j+k,length(pre)-j-k+1);
                                                if not vis(tmp,t) then
                                                        begin
                                                                inc(tail[t]);
                                                                q[t,tail[t]]:=tmp;
                                                                step[t,tail[t]]:=step[t,head[t]]+1;
                                                        end;
                                                check(t);
                                        end;
                        end;
                end;
 end;
Procedure doublebfs;
 begin
        head[0]:=0;head[1]:=0;tail[0]:=1;tail[1]:=1;
        q[0,1]:=int;q[1,1]:=aim;step[0,1]:=0;step[1,1]:=0;
        while (head[0]<tail[0])and(head[1]<tail[1])do if tail[1]<tail[0] then bfs(1) else bfs(0);
 end;
Begin
        init;
        doublebfs;
        writeln('NO');
End.

[pascal 代码]

转载于:https://www.cnblogs.com/FreeDestiny/archive/2011/10/27/2226641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值