初始化:全部初始化为false
边界:lena+lenb<=1 初始化为1
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 55;
bool dp[N][N][N][N];
void solve()
{
string a, b;
cin >> a >> b;
int n = a.size(), m = b.size();
a = " " + a, b = " " + b;
memset(dp, 0, sizeof dp);
int res = 0;
for(int lena=0;lena<=n;lena++)
for(int lenb=0;lenb<=m;lenb++)
for(int la=1;la+lena-1<=n;la++)
for (int lb = 1;lb + lenb - 1 <= m;lb++)
{
int ra = la + lena - 1;
int rb = lb + lenb - 1;
int len = lena + lenb;
bool& v = dp[la][ra][lb][rb];
if (len == 0) continue;
else if (len == 1) dp[la][ra][lb][rb] = true;
else if (len == 2)
{
if (lena && lenb && a[la] == b[lb]) v = true;
else if (lena && !lenb && a[la] == a[ra]) v = true;
else if (!lena && lenb && b[lb] == b[rb]) v = true;
}
else
{
if (lena>1 && a[la] == a[ra])
v |= dp[la + 1][ra - 1][lb][rb];
if (lena && lenb && a[la] == b[rb])
v |= dp[la + 1][ra][lb][rb - 1];
if (lena && lenb && a[ra] == b[lb])
v |= dp[la][ra - 1][lb + 1][rb];
if (lenb && b[lb] == b[rb])
v |= dp[la][ra][lb + 1][rb - 1];
}
if (v)
res = max(res, len);
}
cout << res << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
solve();
return 0;
}