合并回文子串
合并回文子串
https://ac.nowcoder.com/acm/problem/13230
根据题目大致分析组成C的回文子串一定是由A中的子串和B中的子串组成的,而复杂度是允许我们枚举子串的。
所以可以想到区间,
表示字符串
,和字符串
能否构成回文串。
如果,则
如果,则
如果,则
如果,则
对于每个为的状态取最大值即为答案。
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5+10; int dp[55][55][55][55]; char a[55],b[55]; int main() { int t; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); scanf("%s%s",a+1,b+1); int n = strlen(a+1),m =strlen(b+1),ans=0; for(int l1=0; l1<=n; l1++) { for(int l2=0; l2<=m; l2++) { for(int i=1; i+l1-1<=n; i++) { for(int j=1; j+l2-1<=m; j++) { int ii = i+l1-1,jj=j+l2-1; if(l1+l2<=1) dp[i][ii][j][jj]=1; else { if(a[i]==a[ii]) dp[i][ii][j][jj]|=dp[i+1][ii-1][j][jj]; if(b[j]==b[jj]) dp[i][ii][j][jj]|=dp[i][ii][j+1][jj-1]; if(a[i]==b[jj]) dp[i][ii][j][jj]|=dp[i+1][ii][j][jj-1]; if(a[ii]==b[j]) dp[i][ii][j][jj]|=dp[i][ii-1][j+1][jj]; } if(dp[i][ii][j][jj]) ans = max(ans,l1+l2); } } } } printf("%d\n",ans); } }