您的位置:首页 > 其它

poj 3267 字符串dp(倒着DP)

2013-10-22 23:19 204 查看
The Cow Lexicon

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 7397Accepted: 3461
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 lengthL (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 andL

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
USACO 2007 February Silver

这个题目是一道字符串dp题。

状态转移方程如下:从后往前dp

当位置i的字符可以匹配到以它为首的单词时,dp[i] = min(dp[i], dp[i+1]+1, right-i-strlen(dictword)+dp[right])

例如:dcodwe, 当dp到c时,假如可以匹配cow这个单词,则right = 5, left = 1, strlen(dictword) = 3, dp[right] = 1(只有一个e的时候,直接丢弃)

因此dp[1] = 2;

当位置i的字符没有匹配到以它为首的单词时,则dp[i] = min(dp[i], dp[i+1]+1);

提交记录:

1.AC!

代码如下:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: