poj 3267 字符串dp(倒着DP)
2013-10-22 23:19
204 查看
The Cow Lexicon
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
Sample Output
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!
代码如下:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7397 | Accepted: 3461 |
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!
代码如下:
相关文章推荐
- POJ 3267-The Cow Lexicon(dp_字符串)
- POJ 3267-The Cow Lexicon(DP处理字符串)
- poj 1229 字符串模糊匹配dp
- POJ 3267 The Cow Lexicon( DP~去掉最少字符)
- poj 3267 The Cow Lexicon (DP)
- POJ 3280 Cheapest Palindrome (将一个字符串变为回文串的最小代价) 区间dp
- POJ_3267_The Cow Lexicon ( DP )
- POJ 3267 DP
- poj 3267 The Cow Lexicon(dp)
- poj_3267 The Cow Lexicon(dp)
- poj 1187 陨石的秘密(字符串DP)
- poj 3267 The Cow Lexicon dp
- [poj 3267] The Cow Lexicon dp
- POJ 3267:The Cow Lexicon 字符串匹配dp
- POJ 2192 Zipper(判断第3个字符串能否有前两个字符串组成) 水dp
- POJ 3267-The Cow Lexicon(DP)
- POJ 3267:The Cow Lexicon 字符串匹配dp
- POJ 3267 The Cow Lexicon 基础DP
- POJ 3267 The Cow Lexicon (简单DP)
- POJ - 3267 The Cow Lexicon 字符串匹配 (dp)