Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9248 | Accepted: 4389 |
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 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
动态规划
题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列。
PS:是匹配单词序列,而不是一个单词
dp[i]表示从message中第i个字符开始,到第L个字符(结尾处)这段区间所删除的字符数,初始化为dp[L]=0
由于我的程序是从message尾部向头部检索匹配,所以是下面的状态方程:
![](http://hi.csdn.net/attachment/201107/31/0_1312084537WqsU.gif)
从程序可以看出,第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],0到L最少删除的字符数
#include <stdio.h>
#include <string.h>
int W, L;
char message[305];
char dictionary[605][30];
int dp[305];
void DP()
{
int i, j;
dp[L] = 0;//dp[i]表示从i到L所删除的字符数
for(i = L - 1; i >= 0; i--)//从message尾部开始向前检索
{
dp[i] = dp[i + 1] + 1; //字典单词和message无法匹配时,删除的字符数(最坏的情况)
for(j = 0; j < W; j++)//对字典单词枚举
{
int len = strlen(dictionary[j]);
if(len <= L - i && message[i] == dictionary[j][0])//单词长度小于等于当前待匹配message长度
{ //且单词头字母与信息第i个字母相同
int pm = i;//message的指针
int pd = 0;//单词的指针
while(pm < L) //单词逐字匹配
{
if(dictionary[j][pd] == message[pm++])
pd++;
if(pd == len) //字典单词和message可以匹配时,状态优化(更新)
{
dp[i] = dp[i] < (dp[pm] + pm - i - len) ? dp[i] : (dp[pm] + pm - i - len); //dp[pm]表示从pm到L删除的字符数
break; //(pm-i)-pd表示从i到pm删除的字符数
} //则dp[pm]+(pm-i)-pd表示从i到L删除的字符数
}
}
}
}
}
int main()
{
int i;
while(scanf("%d%d", &W, &L) != EOF)
{
scanf("%s", message);
for(i = 0; i < W; i++)
{
scanf("%s", dictionary[i]);
}
DP();
printf("%d\n", dp[0]);
}
return 0;
}