- POJ 3267 - The Cow Lexicon
- Time: 2000MS
- Memory: 65536K
- 难度: 初级
- 分类: 动态规划
0x00 POJ 原题
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
Source
0x10 题目概述
给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列。
是匹配单词序列,而不是一个单词
0x20 解题思路
主要是知道状态方程的含义
dp[i] 表示从 message 中第 i 个字符开始,到第 L 个字符(结尾处)这段区间所删除的字符数,初始化为 dp[L]=0
由于我的程序是从 message 尾部向头部检索匹配,所以是下面的状态方程:

从程序可以看出,第 i 个位置到 L 所删除的字符数,总是先取最坏情况,只有可以匹配单词时才进入第二条方程进行状态优化更新。
第一条方程不难理解,只要弄懂 dp[i] 的意义就能简单推导
第二条方程难点在 dp[pm]+(pm-i)-len
从程序知道,pm 是 message 的指针(其中 i 表示当前所匹配的单词在 message 中的起始位置),pd 是字典的指针
匹配的过程是:
当确认 message 第 i 位和某单词的首位吻合时,就开始逐字匹配,字符相同则两个指针同时向后移动一次,否则 pd 固定,pm 移动。当因为 pm>L 跳出匹配时,说明匹配失败,dp[i] 状态不变;当 pd==单词长度 时,单词匹配成功,进行 dp[i] 的状态优化。
显然,匹配成功时,pm-i 代表匹配过程中,从位置 i 到 pm 的区间长度,再减去单词长度 len,则得到从 i 到 pm 所删除的字符数 (pm-i)-len。又 dp[pm] 表示从 pm 到 L 所删除的字符数(根据检索方向,dp[pm] 的值在此前已经被作为最坏打算处理,因此并不是空值)
从而 dp[pm]+(pm-i)-len 表示 i 到 L 删除的字符数,不难证明这个值一定比 dp[i] 相等或更优,因此取 min 赋值给 dp[i]。
这是本题最难的地方。
最后输出 dp[0] 就可以了,dp[0] 的意思相信大家都明白了。
0x30 AC 源码
//Memory Time
//336K 79MS
#include<iostream>
#include<string>
using namespace std;
int min(int a,int b)
{
return a<b?a:b;
}
int main(int i,int j)
{
int w,L;
while(cin>>w>>L)
{
/*Read In*/
int* dp=new int[L+1];
char* mesg=new char[L];
string* dict=new string[w];
cin>>mesg;
for(i=0;i<w;i++)
cin>>dict[i];
/*Initial*/
dp[L]=0; //dp[i]表示从i到L所删除的字符数
/*Dp-Enum*/
for(i=L-1;i>=0;i--) //从message尾部开始向前检索
{
dp[i]=dp[i+1]+1; //字典单词和message无法匹配时,删除的字符数(最坏的情况)
for(j=0;j<w;j++) //对字典单词枚举
{
int len=dict[j].length();
if(len<=L-i && dict[j][0]==mesg[i]) //单词长度小于等于当前待匹配message长度
{ //且单词头字母与信息第i个字母相同
int pm=i; //message的指针
int pd=0; //单词的指针
while(pm<L) //单词逐字匹配
{
if(dict[j][pd]==mesg[pm++])
pd++;
if(pd==len)
{ //字典单词和message可以匹配时,状态优化(更新)
dp[i]=min(dp[i],dp[pm]+(pm-i)-len);//dp[pm]表示从pm到L删除的字符数
break; //(pm-i)-pd表示从i到pm删除的字符数
} //则dp[pm]+(pm-i)-pd表示从i到L删除的字符数
}
}
}
}
cout<<dp[0]<<endl;
delete dp,mesg,dict;
}
return 0;
}