跳到正文

个人记录

BD2508T1

OI___Lover约 3 分钟
原文

这是 2025年11月14日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月24日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1
C++
#include<bits/stdc++.h>
using namespace std;
const int N=53;
string a,b;
bool dp[N][N][N][N];
int main(){
	//freopen("ex_a2.in","r",stdin);
	int T,n,m;
	cin>>T;
	while(T--){
		cin>>a>>b;
		int ans=0;
		n=a.size(),m=b.size();
		for(int l1=n+2;l1>=2;l1--){
			for(int r1=l1-2;r1<=n+1;r1++){
				for(int l2=m+2;l2>=2;l2--){
					for(int r2=l2-2;r2<=m+1;r2++){
						if(l1>r1&&l2>r2){
							dp[l1][r1][l2][r2]=1;
							ans=max(ans,0);
							//cout<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<' '<<dp[l1][r1][l2][r2]<<'\n';
							continue;
						}
						if(l1>r1){
							dp[l1][r1][l2][r2]=((b[l2-2]==b[r2-2])&&dp[l1][r1][l2+1][r2-1]);
							if(dp[l1][r1][l2][r2])ans=max(ans,r1-l1+1+r2-l2+1);
							//cout<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<' '<<dp[l1][r1][l2][r2]<<'\n';
							continue;
						}
						if(l2>r2){
							dp[l1][r1][l2][r2]=((a[l1-2]==a[r1-2])&&dp[l1+1][r1-1][l2][r2]);
							if(dp[l1][r1][l2][r2]){
								ans=max(ans,r1-l1+1+r2-l2+1);
							}
							//cout<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<' '<<dp[l1][r1][l2][r2]<<'\n';
							continue;
						}
						dp[l1][r1][l2][r2]=(dp[l1+1][r2][l2][r2-1]&(a[l1-2]==b[r2-2]))|(dp[l1][r1-1][l2+1][r2]&(a[r1-2]==b[l2-2]));
						if(r1>l1){
							dp[l1][r1][l2][r2]|=((a[l1-2]==a[r1-2])&&dp[l1+1][r1-1][l2][r2]);
						}
						if(r2>l2){
							dp[l1][r1][l2][r2]|=((b[l2-2]==b[r2-2])&&dp[l1][r1][l2+1][r2-1]);
						}
						if(dp[l1][r1][l2][r2]){
							ans=max(ans,r1-l1+1+r2-l2+1);
						}
						//cout<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<' '<<dp[l1][r1][l2][r2]<<'\n';
					}
				} 
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}