poj-3267-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 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

题意:给一个有L个字符的mesg主字符串,再给出w个单词,要求出在主字符串中删去最少多少个字符能使其匹配到单词中。

用dp[i]表示字符串从i到L所删除的字符串,从主字符串尾部向前检索字母,想要含有短字符串,必须保证单词长度小于当前匹配字符串长度且单词头字母与当前字母相同。

dp[i]=dp[i+1]+1(表示不能匹配(最坏情况,所有字符全部删除))
dp[i]=min(do[i],dp[pm]+(pm-i)-pd)(可以匹配时,取最优)

先取最坏状态,只有可匹配时才取最优

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]

参考自:http://blog.csdn.net/lyy289065406/article/details/6648121

代码:

#include <stdio.h>  
#include <string.h>  
#define min(a,b) a<b?a:b
int dp[320];
char mesg[320];  
char dict[620][30]; 
int main()  
{
    int w,L,i,j;
    scanf("%d%d",&w,&L);

    scanf("%s",mesg);  
    for(i=0;i<w;i++)
        scanf("%s",dict[i]);
    dp[L]=0;

    for(i=L-1;i>=0;i--)  //从message尾部开始向前检索  
    {  
        dp[i]=dp[i+1]+1;  //字典单词和message无法匹配时,删除的字符数(最坏的情况)  
        for(j=0;j<w;j++) //对字典单词枚举  
        {  
            int len=strlen(dict[j]);  
            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删除的字符数  
                                                           //(pm-i)-pd表示从i到pm删除的字符数 
                                                           //则dp[pm]+(pm-i)-pd表示从i到L删除的字符数
                        break;                            
                    }                                     
                }
            }
        }
    }
    printf("%d\n",dp[0]); 
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值