455人阅读 评论(1)

# 题解

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);
}
}
0
0

个人资料
• 访问：359784次
• 积分：11984
• 等级：
• 排名：第1484名
• 原创：815篇
• 转载：4篇
• 译文：0篇
• 评论：212条
最新评论
文章分类