# [牛客每日一题3月26日][NC13230]合并回文子串

 1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 const int maxn = 2e5+10;
5 int dp[55][55][55][55];
6 char a[55],b[55];
7 int main() {
8     int t;
9     scanf("%d",&t);
10     while(t--) {
11         memset(dp,0,sizeof(dp));
12         scanf("%s%s",a+1,b+1);
13         int n  = strlen(a+1),m =strlen(b+1),ans=0;
14         for(int l1=0; l1<=n; l1++) {
15             for(int l2=0; l2<=m; l2++) {
16                 for(int i=1; i+l1-1<=n; i++) {
17                     for(int j=1; j+l2-1<=m; j++) {
18                         int ii = i+l1-1,jj=j+l2-1;
19                         if(l1+l2<=1)
20                             dp[i][ii][j][jj]=1;
21                         else {
22                             if(a[i]==a[ii]) dp[i][ii][j][jj]|=dp[i+1][ii-1][j][jj];
23                             if(b[j]==b[jj]) dp[i][ii][j][jj]|=dp[i][ii][j+1][jj-1];
24                             if(a[i]==b[jj]) dp[i][ii][j][jj]|=dp[i+1][ii][j][jj-1];
25                             if(a[ii]==b[j]) dp[i][ii][j][jj]|=dp[i][ii-1][j+1][jj];
26                         }
27                         if(dp[i][ii][j][jj])
28                             ans = max(ans,l1+l2);
29                     }
30                 }
31             }
32         }
33         printf("%d\n",ans);
34     }
35 }

posted @ 2020-03-26 14:35  祈梦生  阅读(66)  评论(0编辑  收藏