zoukankan      html  css  js  c++  java
  • poj2192

    初看这道题,以为是先用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
  • 相关阅读:
    Dubbo简介---搭建一个最简单的Demo框架
    git学习总结
    FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
    Spring AOP实现Mysql数据库主从切换(一主多从)
    Mybatis中int insertSelective()的相关问题
    主从数据库读写分离知识
    IoC理解
    AOP理解
    MyBatis中mybatis-generator代码生成的一般过程
    fread 快速读入
  • 原文地址:https://www.cnblogs.com/phile/p/4473311.html
Copyright © 2011-2022 走看看