码迷,mamicode.com
首页 > 其他好文 > 详细

NC13230 合并回文子串(区间dp)

时间:2020-03-24 23:10:51      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:范围   span   字符   mes   nbsp   open   isp   bsp   并且   

从数据范围不难推出可以用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

 

NC13230 合并回文子串(区间dp)

标签:范围   span   字符   mes   nbsp   open   isp   bsp   并且   

原文地址:https://www.cnblogs.com/ctyakwf/p/12562610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!