POJ3276 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

翻译

输入字典含有词组的个数,主串的字符数
输入主串,依次输入字典的字符
求删除至少多少个字符可以让主串全部由字典中的词组组成

思路

动态规划了解比较好的皆有直接看代码了,解释很啰嗦!!!

动态规划,把大问题分成小问题,所以是从后往前求解

发现很多题解都是这样写但是没有具体原因,也有很多人对这个有疑问,这不就是根据动态规划的
基本思想来决定的么,从后往前保证没走一个字符有唯一的解,1、新出字符后组成一个新的与字
典相符的子串2、新出字符需要删去。这两个情况需要删去字符数的最小值

我们从后前遍历主串,并遍历字典,如果此时新字符到最后的字符数>=当前字典词组的字符数(字符数一共都没这个词组的字符数多怎么可能由这个词组组成)且新字符与此词组的第一个字符相同则有可能删去一些字符后由此词组组成(此词组的首字母是主串的新字符)。然后开始从主串的新字符往后遍历,直到出现出现这个当前词组,然后通过状态转移方程计算是不是最优解。

动态转移方程是dp[i]=min(dp[i],dp[t]+(t-i)-len);

dp[i]是从第i个字符到最后需要删除的元素个数

我们来表示一下此题是怎样动态规划的

1、w和white首字母相同但是总字符数(1)小于此词组字符数(5)所以不行dp[l-1]直接加
一。
2、往前遍历到c时发现与词组cow首字母相同,且总字符数(4)大于此词组数(3)所以开始
遍历主串,我们遍历了主串的4个字符后,与词组相符的字符数达到要求,我们需要删去的字符数
即为(t-i)-len。(t:主串遍历的字符数、i:新字符所在位置、len:当前字典词组字符数)
然后比较是否我们选择让这个新字符组成一个词组,得到最优解。
同理我们不断往前得到最优解

CODE:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char dic[601][30],s[301];
int dp[301];//dp[i]是从第i个字符到最后需要删除的元素个数
int main()
{
    int n,l;
    while(cin >> n >> l)
    {
        cin >> s;
        for(int i=0;i<n;i++)
            cin >> dic[i];
        memset(dp,0,sizeof(dp));
        dp[l]=0;//最后一个的后一个初始化为0
        for(int i=l-1;i>=0;i--)//从后往前比较,遵守由简变繁
        {
            dp[i]=dp[i+1]+1;//先假设当前字符不属于字典,即假设他要被删去
            for(int j=0;j<n;j++)//遍历字典
            {
                int len=strlen(dic[j]);
                if(len<=l-i&&dic[j][0]==s[i])//主串剩余的字符比当前字典字符多的话才有必要计算,否则这个肯定不属于字典
                {
                    int t=i;
                    int count=0;
                    while(t<l)
                    {
                        if(dic[j][count]==s[t++])
                            count++;
                        if(count==len)//遍历出此词组
                        {
                            dp[i]=min(dp[i],dp[t]+(t-i)-len);
                            //决定是不是要让新字符组成一个词组
                            break;
                        }
                    }
                }
            }
        }
        cout << dp[0] << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值