首先,我们把字符串反转,然后用反串和原串求最大公共子序列,再用字符串长度减去最大公共子序列的长度就是答案,我们还可以用滚动数组优化内存
状态转移方程:
(i长度的a串和j长度的b串的最长公共子序列长度)
1.dp[i-1][j-1]+1 a[i]=b[j]
dp[i][j]=
2.max(dp[i][j-1],dp[i-1][j]) a[i]!=b[j]
#include
#include
#include
#include
#include
#include<string>
using namespace std;
int re[2][1005];//使用滚动数组优化了空间
int main()
{int t,i,j;string a,b;scanf("%d",&t);while(t--){cin>>a;b&#61;a;reverse(a.begin(),a.end());memset(re,0,sizeof(re));for(i&#61;0;i
}printf("%d\n",a.length()-re[(a.length())&1][b.length()]);}return 0;
}