https://blog.csdn.net/dreaming__ldx/article/details/88069272
https://blog.csdn.net/dingduan9147/article/details/101934455
#include<bits/stdc++.h> #define ri register int using namespace std; const int N=10005,M=1005; char s[N],t[M]; int tmp=0,ans=0,f[2][M],n,m,fail[M]; int main() { scanf("%s%s",s+1,t+1),n=strlen(s+1),m=strlen(t+1); for(ri i=1,j=0;i<=m;++i) { while(j&&t[i+1]!=t[j+1]) j=fail[j]; fail[i+1]=t[i+1]==t[j+1]?++j:0; } memset(f[tmp],-1,sizeof(f[tmp])); f[tmp][0]=0; for(ri i=1;i<=n;++i) { memset(f[tmp^1],-1,sizeof(f[tmp^1])); for(ri j=0,k;j<m;++j)if(~f[tmp][j]) { f[tmp^1][j]=max(f[tmp^1][j],f[tmp][j]); k=j; while(k&&s[i]!=t[k+1]) k=fail[k]; if(s[i]==t[k+1]) ++k; f[tmp^1][k]=max(f[tmp^1][k],f[tmp][j]+1); } tmp^=1; } for(ri i=0;i<m;++i)ans=max(ans,f[tmp][i]); cout<<n-ans; return 0; }
原文:https://www.cnblogs.com/cutemush/p/12616560.html