初看这道题,以为是先用SA和SC求LCS,再从SC中把SA剔除,看剩下来的是不是SB(……)

显然不对;因为SC与SA的公共子串不止一种,判断不一定正确。

于是考虑SC中每一个字符,如果能够匹配,那么不是SA中的就是SB中的;

定义bool s[i,j]为SA前i个,SB前j个能否组成SC前i+j个;

所以s[i,j]=true (s[i-1,j] and SA[i]=s[i+j]) or (s[i,j-1] and SB[j]=s[i+j]);

则最后能匹配的条件为s[length(SA),length(SB)]=true

 1 var f:array[0..500,0..500] of boolean;
 2     ff:array[0..500] of boolean;
 3     xx,s1,s2,s3:ansistring;
 4     l1,l2,l3,k,i,j,p,n,x,y:integer;
 5     check:boolean;
 6 function max(a,b:integer):integer;
 7   begin
 8     if a>b then max:=a else max:=b;
 9   end;
10 
11 begin
12   readln(n);
13   for k:=1 to n do
14   begin
15     readln(xx);
16     xx:=xx+' ';
17     p:=pos(' ',xx);
18     s1:=copy(xx,1,p-1);
19     xx:=copy(xx,p+1,length(xx));
20     p:=pos(' ',xx);
21     s2:=copy(xx,1,p-1);
22     xx:=copy(xx,p+1,length(xx));
23     p:=pos(' ',xx);
24     s3:=copy(xx,1,p-1);
25     fillchar(f,sizeof(f),false);
26     l1:=length(s1);
27     l2:=length(s2);
28     l3:=length(s3);
29     f[0,0]:=true;
30     if s1[1]=s3[1] then f[1,0]:=true;
31     if s2[1]=s3[1] then f[0,1]:=true;
32     for i:=0 to l1 do
33       for j:=0 to l2 do
34       begin
35         if (s1[i]=s3[i+j]) and f[i-1,j] then f[i,j]:=true;
36         if (s2[j]=s3[i+j]) and f[i,j-1] then f[i,j]:=true;
37       end;
38     if f[l1,l2] then writeln('Data set ',k,': yes')
39     else writeln('Data set ',k,': no');
40   end;
41 end.
View Code

 

posted on 2013-11-30 15:54  acphile  阅读(211)  评论(0编辑  收藏  举报