# 题解

dp转移显然。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
const int maxn=50+10,inf=100000000;
char a[maxn],b[maxn];
int fa[maxn][maxn],fb[maxn][maxn],f[maxn][maxn][maxn][maxn];
int i,j,i1,i2,jj1,j2,k,l,t,n,m,ca,ans;
int main(){
scanf("%d",&ca);
while (ca--){
scanf("%s",a+1);
scanf("%s",b+1);
n=strlen(a+1);m=strlen(b+1);
fo(i,0,n)
fo(j,0,n)
fa[i][j]=-inf;
fo(i,0,m)
fo(j,0,m)
fb[i][j]=-inf;
fo(i1,0,n)
fo(i2,0,n)
fo(jj1,0,m)
fo(j2,0,m)
f[i1][i2][jj1][j2]=-inf;
fo(i,1,n)
fo(j,1,m)
if (a[i]==b[j]) f[i][i][j][j]=2;
ans=0;
fo(i,1,n) fa[i][i]=1;
fo(i,1,n-1)
if (a[i]==a[i+1]) fa[i][i+1]=2;
fd(i,n,1)
fo(j,1,n)
if (fa[i][j]!=-inf){
t=fa[i][j];
ans=max(ans,t);
if (i>1&&j<n&&a[i-1]==a[j+1]) fa[i-1][j+1]=t+2;
fo(k,1,m){
if (j<n&&b[k]==a[j+1]) f[i][j+1][k][k]=t+2;
if (i>1&&b[k]==a[i-1]) f[i-1][j][k][k]=t+2;
if (k<m&&b[k]==b[k+1]) f[i][j][k][k+1]=t+2;
}
}
fo(i,1,m) fb[i][i]=1;
fo(i,1,m-1)
if (b[i]==b[i+1]) fb[i][i+1]=2;
fd(i,m,1)
fo(j,1,m)
if (fb[i][j]!=-inf){
t=fb[i][j];
ans=max(ans,t);
if (i>1&&j<m&&b[i-1]==b[j+1]) fb[i-1][j+1]=t+2;
fo(k,1,n){
if (j<m&&a[k]==b[j+1]) f[k][k][i][j+1]=t+2;
if (i>1&&a[k]==b[i-1]) f[k][k][i-1][j]=t+2;
if (k<n&&a[k]==a[k+1]) f[k][k+1][i][j]=t+2;
}
}
fd(i1,n,1)
fo(i2,1,n)
fd(jj1,m,1)
fo(j2,1,m)
if (f[i1][i2][jj1][j2]!=-inf){
t=f[i1][i2][jj1][j2];
ans=max(ans,t);
if (ans==6){
t=t;
}
if (i1>1&&i2<n&&a[i1-1]==a[i2+1]) f[i1-1][i2+1][jj1][j2]=t+2;
if (jj1>1&&j2<m&&b[jj1-1]==b[j2+1]) f[i1][i2][jj1-1][j2+1]=t+2;
if (i1>1&&j2<m&&a[i1-1]==b[j2+1]) f[i1-1][i2][jj1][j2+1]=t+2;
if (i2<n&&jj1>1&&b[jj1-1]==a[i2+1]) f[i1][i2+1][jj1-1][j2]=t+2;
}
printf("%d\n",ans);
}
}

#### CodeM美团点评编程大赛 初赛B轮 A 黑白树 贪心 E 子串 暴力

2017-06-25 17:36:42

#### 「美团 CodeM 初赛 Round B」子串

2017-07-04 18:27:17

#### CodeM美团点评编程大赛初赛B轮题解

2017-06-28 23:20:55

#### 美团 CodeM 资格赛 Round A 合并回文子串

2017-06-25 17:52:59

#### 美团CodeM 初赛 A伦 合并回文子串 区间dp

2017-06-19 14:40:12

#### [LOJ#6159][美团 CodeM 初赛 Round A][暴力即正解]最长树链

2017-07-06 00:47:18

#### 美团CodeM 初赛 A轮 数列互质 莫队

2017-06-20 20:30:27

#### [CodeM初赛A轮]D

2017-06-18 22:18:59

#### CodeM资格赛 Round A 最长树链

2017-07-01 23:47:48

[CodeM初赛A轮]B