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