题目描述
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.
没有几个人知道,奶牛有她们自己的字典,里面的有W (1 ≤ W ≤ 600)个词,每个词的长度不超过25,且由小写字母组成.她们在交流时,由于各种原因,用词总是不那么准确.比如,贝茜听到有人对她说”browndcodw”,确切的意思是”browncow”,多出了两个”d”,这两个”d”大概是身边的噪音.
奶牛们发觉辨认那些奇怪的信息很费劲,所以她们就想让你帮忙辨认一条收到的消息,即一个只包含小写字母且长度为L (2 ≤ L ≤ 300)的字符串.有些时候,这个字符串里会有多余的字母,你的任务就是找出最少去掉几个字母就可以使这个字符串变成准确的”牛语”(即奶牛字典中某些词的一个排列).
输入输出格式
输入格式:
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
• 第1行:两个用空格隔开的整数,W和L.
• 第2行:一个长度为L的字符串,表示收到的信息.
• 第3行至第W+2行:奶牛的字典,每行一个词.
输出格式:
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.
一个整数,表示最少去掉几个字母就可以使之变成准确的”牛语”.
输入输出样例
输入样例#1: 复制
6 10
browndcodw
cow
milk
white
black
brown
farmer
输出样例#1: 复制
2
说明
感谢@ws_fuweidong 提供完整题面
思路
题意:计算至少需要删除多少个字母才能使该序列正好由给出的单词组成
设dp[i]代表的是从i~Len这部分序列中最少所要删除的单词数,初始化dp[Len]=0。状态转移方程如下:
$$dp[i]=dp[i+1]+1 (不匹配,直接删)$$
$$dp[i]=min(dp[i],dp[p1]+(p1-i)-len) (能的话就取最优)$$
理解:
第一个很好理解,就是在不能匹配的情况下,在上一个的基础上多删除一个,也就是删除新加入的单词。
第二个有点复杂,len就是单词的长度,p1相当于是指向序列的指针,p1-i代表的是包含当前单词的序列的长度,因为当中可能还掺杂着别的一些单词,p1-i-len代表的就是多余单词的个数,也就是需要删除个数(比如说,当前序列为codw,比较单词为cow,p1-i=4,p1-i-len=1,也就是要删除d这一个多余的单词)。dp[p1]显然就是序列为p1~Len时最少所要删除的单词数。
代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<cmath> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #define re register int using namespace std; inline int read(){ int x=0,w=1; char s=getchar(); while(s!='-'&&(s<'0'||s>'9')) s=getchar(); if(s=='-') w=-1,s=getchar(); while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+s-48,s=getchar(); return x*w; } char words[605][30]; char s[305]; int dp[305]; int main() { int W, L; cin>>W>>L; cin>>s; for(re i=0;i<W;i++) cin>>words[i]; dp[L]=0; for(re i=L-1;i>=0;i--) { dp[i]=dp[i+1]+1; //无法匹配时需要删除的字符数,先记录一下最坏情况 for (int j=0;j<W;j++) { int len=strlen(words[j]); if (len<=L-i&&words[j][0]==s[i]) { //单词长度不能大于i~L字段的长度并且首字母得相同 int p1=i; int p2=0; while(p1<L){ if (s[p1]==words[j][p2]) { p1++; p2++; } else p1++; if (p2==len) { dp[i]=min(dp[i],dp[p1]+p1-i-len); break; } } } } } cout << dp[0] << endl; return 0; }