POJ 3267:The Cow Lexicon(DP)

时间:2024-01-08 22:41:44
The Cow Lexicon
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9380   Accepted: 4469

Description

Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no more 25 of the characters 'a'..'z'. Their cowmunication system, based on mooing, is not very accurate; sometimes they hear words that do not make any sense. For instance, Bessie once received a message that said "browndcodw". As it turns out, the intended message was "browncow" and the two letter "d"s were noise from other parts of the barnyard.

The cows want you to help them decipher a received message (also containing only characters in the range 'a'..'z') of length L (2 ≤ L ≤ 300) characters that is a bit garbled. In particular, they know that the message has some extra letters, and they want you to determine the smallest number of letters that must be removed to make the message a sequence of words from the dictionary.

Input

Line 1: Two space-separated integers, respectively: W and L  Line 2: L characters (followed by a newline, of course): the received message  Lines 3..W+2: The cows' dictionary, one word per line

Output

Line 1: a single integer that is the smallest number of characters that need to be removed to make the message a sequence of dictionary words.

Sample Input

6 10
browndcodw
cow
milk
white
black
brown
farmer

Sample Output

2

  题意:给出一个串s,还有n个子串str[n],然后求s串要删除多少个字符才能让s串匹配了str[n]里的串后没有多余字符(有点表意不清,大概能懂就好)。
 /*
dp数组的意义:从s中第i个字符开始,到尾部这段区间所删除的字符数,初始dp[len]=0;
转移方程:1) dp[i]=dp[i+1]+1 最坏情况下,每次都无法匹配
2) dp[i]=min(dp[i],dp[y]+(y-i)-l) 若有一个字符串能够匹配成功,那么
就取最优。含义:该匹配是从i到y进行匹配的,s串扫过的长度为y-i,
其中有l个字符可以匹配,因此剩下(y-i)-l个字符无法进行匹配。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <iostream>
using namespace std; char s[],a[][];
int dp[]; int main()
{
int n,len;
cin>>n>>len;
cin>>s;
for(int i=;i<n;i++){
scanf("%s",a[i]);
}
int j,cnt;
dp[len]=;
for(int i=len-;i>=;i--){
dp[i]=dp[i+]+;
for(j=;j<n;j++){
int l=strlen(a[j]);
if(l<=len-i&&a[j][]==s[i]){
int x=,y=i;
while(y<len){
if(a[j][x]==s[y++]){
x++;
}
if(x==l){
dp[i]=min(dp[i],dp[y]+(y-i)-l);
break;
}
}
}
}
}
cout<<dp[]<<endl;
return ;
}

2016-05-29