题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列。
PS:是匹配单词序列,而不是一个单词
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
const int maxn = 605;
int W,L;
int dp[maxn],len[maxn];
string str,word[maxn];
int main()
{
scanf("%d%d",&W,&L);
cin>>str;
for( int i = 0; i < W; i ++ )
cin>>word[i];
dp[L] = 0;
for( int i = L-1; i >= 0; i -- ){
dp[i] = dp[i+1] + 1;
for( int j = 0; j < W; j ++ ){
if( word[j].length() <= L-i && word[j].at(0) == str[i] ){
int p = i,q = 0; //p指向str q指向word
while( p < L ){
if( word[j].at(q) == str[p++] )
q ++;
if( q == word[j].length() ){
dp[i] = min(dp[p]+p-i-q,dp[i] );
break;
}
}
}
}
}
printf("%d\n",dp[0]);
return 0;
}