f[i]=min{f[i+1]+1,f[i+len[j]+cant]+cant}(for i=L-1 downto 0)(1<=j<=w)
#include<cstdio> #include<iostream> #include<string> using namespace std; int n,m; string s,words[601]; int f[302]; int main() { scanf("%d%d",&n,&m); cin>>s; for(int i=1;i<=n;++i) cin>>words[i]; for(int i=m-1;i>=0;--i) { f[i]=f[i+1]+1; for(int j=1;j<=n;++j) { int now=0,cant=0; for(int k=i;k<m;++k) { if(words[j][now]==s[k]) ++now; else ++cant; if(now==words[j].length()) break; } if(now==words[j].length()) f[i]=min(f[i],f[i+cant+now]+cant); } } cout<<f[0]<<endl; return 0; }
时间: 2024-10-09 13:33:12