poj3267The Cow Lexicon(简单dp 虽然我并不是很会)

The Cow Lexicon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9765 Accepted: 4681

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

解题思路

动态规划

 

题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列。

PS:是匹配单词序列,而不是一个单词

 

不多说,看程序

主要是知道状态方程的含义

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]

这是本题最难的地方

#include<stdio.h>
#include<string.h>
#define min(a,b) a>b?b:a
//char map[750][750];
char str[750][750];
char s[750];
int len[750];
int dp[750];

int main()
{
    int n,l,i,j;
    while(~scanf("%d%d",&n,&l))
    {
        memset(dp,0,sizeof(dp));
        scanf("%s",s);

        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            len[i]=strlen(str[i]);//记录每个单词的长度
        }

        for(i=l-1;i>=0;i--)
        {
            dp[i]=dp[i+1] + 1;//最坏的一种情况 即单词需要删除
            for(j=0;j<n;j++)
            {
                if(str[j][0] ==s[i]&&i+len[j]<=l)//单词长度需要小于待匹配s中的长度且首字母需要相同
                {
                    int ss=i;
                    int t=0;
                    while(ss<l)
                    {
                        if(str[j][t]==s[ss++])//如果想同t就加一
                        {
                            t++;
                        }
                        if(t==len[j])//如果t等于单词的长度
                        {
                            dp[i]=min(dp[i],dp[ss]+ss-i-len[j]);//对dp进行优化,ss-i-len[j]既为需要删除的长度
                            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、付费专栏及课程。

余额充值