zoukankan      html  css  js  c++  java
  • NC13230 合并回文子串(区间dp)

    从数据范围不难推出可以用f[][][][],表示由两个字符串来表示的最长大小

    并且因为是回文串,所以我们要向头尾加字符,来变大,因为这个是回文子串,也就是连续的一段。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<string>
    #include<cstring>
    using namespace std;
    const int N=55;
    char s1[55],s2[55];
    int f[N][N][N][N];
    int main(){
        int t;
        cin>>t;
        while(t--){
            scanf("%s%s",s1+1,s2+1);
            int n=strlen(s1+1);int m=strlen(s2+1);
            int len1,len2,l1,l2;
            int res=1;
            for(len1=0;len1<=n;len1++){
                for(len2=0;len2<=m;len2++){
                    for(l1=1;l1+len1-1<=n;l1++){
                        for(l2=1;l2+len2-1<=m;l2++){
                           int r1=l1+len1-1;
                           int r2=l2+len2-1;
                            if(len1+len2<=1){
                                f[l1][r1][l2][r2]=1;
                            }
                            else{
                                f[l1][r1][l2][r2]=0;
                                if(len1>1&&s1[l1]==s1[r1])
                                    f[l1][r1][l2][r2]|=f[l1+1][r1-1][l2][r2];
                                if(len2>1&&s2[l2]==s2[r2])
                                    f[l1][r1][l2][r2]|=f[l1][r1][l2+1][r2-1];
                                if(len1&&len2&&s1[l1]==s2[r2])
                                    f[l1][r1][l2][r2]|=f[l1+1][r1][l2][r2-1];
                                if(len1&&len2&&s1[r1]==s2[l2])
                                    f[l1][r1][l2][r2]|=f[l1][r1-1][l2+1][r2];
                            }
                            if(f[l1][r1][l2][r2])
                                res=max(res,r1-l1+1+r2-l2+1);
                        }
                    }
                }
            }
            cout<<res<<endl;
        }
    }
    View Code
  • 相关阅读:
    linux scull 函数open 方法
    linux scull 中的设备注册
    linux 字符设备注册
    linux inode 结构
    linux设备驱动文件结构
    linux一些重要数据结构
    Python3.2官方文档翻译--输出格式化
    1021. Deepest Root (25)
    hdu 4779 Tower Defense (思维+组合数学)
    cookie是什么? -- web
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/12562610.html
Copyright © 2011-2022 走看看