poj3267

题目链接:

啊哈哈,选我

题目:

The Cow Lexicon

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7963   Accepted: 3734

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

Source

USACO 2007 February Silver

这里有个链接讲解的非常仔细,值得借鉴。

传送门

因为讲解的非常仔细,我说一下思路。。就是逆向枚举,看能否匹配,能匹配就进行更新,不能匹配就是最坏的情况,进行删除操作。。

代码为:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=300+10;
char dic[600+10][25+10];
char mes[maxn];
int dp[maxn];
int w,l;
int main()
{
    while(~scanf("%d%d",&w,&l))
    {
        scanf("%s",mes);
        for(int i=1;i<=w;i++)
            scanf("%s",dic[i]);
        int dicpd,mespd;
        memset(dp,INF,sizeof(dp));
        dp[l]=0;
        for(int i=l-1;i>=0;i--)
        {
            dp[i]=dp[i+1]+1;
            for(int j=1;j<=w;j++)
            {
                int len=strlen(dic[j]);
                if(l-i>=len&&mes[i]==dic[j][0])
                {
                   mespd=i,dicpd=0;
                   while(mespd<l)
                   {
                        if(mes[mespd]==dic[j][dicpd])
                              dicpd++;
                        mespd++;
                        if(dicpd==len)
                         {
                            dp[i]=min(dp[i],dp[mespd]+mespd-dicpd-i);
                            break;
                        }
                   }
                }
            }
        }
        printf("%d\n",dp[0]);
    }
    return 0;
}

poj3267,布布扣,bubuko.com

时间: 2024-08-25 04:52:32

poj3267的相关文章

poj3267——线性dp

poj3267——线性dp The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8458   Accepted: 3993 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'

poj3267(The Cow Lexicon)

题目地址:The Cow Lexicon 题目大意: 奶牛有自己的识别单词的语言,它有自己的字典序列,如果给一串字符不符合奶牛的字典里的单词,奶牛就无法识别,你的任务就是找出给的字符串中包含给出奶牛字典的单词,至少从主串里删除几个字符,使主串只包含奶牛字典里的单词,不包含多于的字符. 解题思路: 动态规划dp. DP一直都不太懂,不看解题报告根本推不出来状态方程,入门水平都不够,需要好好练习学习该知识点. 状态方程: dp[i] i 代表从i到L最少需要删除的字符个数. 首先分为两种思想: 一是

POJ3267——The Cow Lexicon(动态规划)

The Cow Lexicon DescriptionFew 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 word

POJ3267 The Cow Lexicon(DP+删词)

The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9041   Accepted: 4293 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 c

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

ACM算法总结及刷题参考

参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递推.     (5)构造法.(po

POJ题目推荐(转载)

POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求,当然有能力的同学可以直接切掉.2.标记为A and B的题目是比较相似的题目,建议大家两个一起做,可以对比总结,且二者算作一个题目.3.列表中大约有70个题目.大家选做其中的50道,且每类题目有最低数量限制.4.这里不少题目在BUPT ACM FTP上面都有代码,请大家合理利用资源.5.50个题目要求每个题目都要写总结,养成良好的习惯.6.这个列表的目的在于让大家对各个方面的算法有个了解,也许要求有些苛刻,教条,请大家谅

POJ题目分类

初期: 一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,he

嗷嗷嗷,kuangbin大大博客上拉的题

正在学(learning),未学(waiting),已学(cut  vovering) 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.