Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8747 | Accepted: 4124 |
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 2: L characters (followed by a newline, of course): the received message
Lines 3.. W+2: The cows' dictionary, one word per line
Output
Sample Input
6 10 browndcodw cow milk white black brown farmer
Sample Output
2
Source
给出一个字符串和一些单词,问最少去掉几个字母能使剩下的字符串全由这些单词构成
一道典型的dp题,转移方程是
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int w,l;
int dp[2333];
char key[2333];
char dic[2333][233];
int min(int a,int b)
{
if(a<b) return a;
else return b;
}
int main()
{
cin>>w>>l;
scanf("%s",key);
for(int i=1;i<=w;i++)
{
scanf("%s",dic[i]);
}
dp[0]=1;
for(int i=1;i<l;i++)
{
dp[i]=dp[i-1]+1;
int len;int k;
for(int j=1;j<=w;j++)
{
len=strlen(dic[j])-1;
k=i;
if(len>i) continue;
while(len>=0&&k>=0&&k>=len)
{
if(key[k]==dic[j][len]) len--;
k--;
}
if(len<0) dp[i]=min(dp[i],dp[k]+i-k-strlen(dic[j]));
}
}
cout<<dp[l-1];
return 0;
}