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

dp[i]表示message从i位置开始需要删除几个字母

dp[i] = dp[i + 1] + 1 // 表示它在i位置并不匹配,删除i位置的字母,

dp[i] = min(dp[i],dp[i + len + t] + t) //对于长度为len的单词,从i开始需要删除t个字母才能完全匹配,i+len+t表示匹配后的下一个,dp[i+len+t]就是匹配成功下一个位置的需要删除的字母数

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
char dic[][];
char mess[ + ];
int w,l,dp[];
int DP(int x, int len, int y)
{
int j = , tot = ;
while(x <= l)
{
if(mess[x] == dic[y][j])
j++;
else
tot++;
if(j == len + ) // j==len的时候还没结束,还要匹配len位置,len是最后一个
return tot;
x++;
}
return -;
}
int main()
{
while(scanf("%d%d", &w, &l) != EOF)
{
scanf("%s", mess + );
for(int i = ; i <= w; i++)
{
scanf("%s", dic[i] + );
}
memset(dp, , sizeof(dp));
for(int i = l; i > ; i--)
{
dp[i] = dp[i + ] + ;
for(int j = ; j <= w; j++)
{
int len = strlen(dic[j] + );
int t = DP(i, len, j);
if(t != -)
{
dp[i] = min(dp[i], dp[i + len + t] + t);
}
}
}
printf("%d\n", dp[]);
}
return ;
}

POJ3267 The Cow Lexicon(DP+删词)的更多相关文章

  1. POJ3267 The Cow Lexicon(dp)

    题目链接. 分析: dp[i]表示母串从第i位起始的后缀所对应的最少去掉字母数. dp[i] = min(dp[i+res]+res-strlen(pa[j])); 其中res 为从第 i 位开始匹配 ...

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

    The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...

  3. poj3267--The Cow Lexicon(dp:字符串组合)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8211   Accepted: 3864 D ...

  4. POJ 3267-The Cow Lexicon(DP)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8252   Accepted: 3888 D ...

  5. USACO 2007 February Silver The Cow Lexicon /// DP oj24258

    题目大意: 输入w,l: w是接下来的字典内的单词个数,l为目标字符串长度 输入目标字符串 接下来w行,输入字典内的各个单词 输出目标字符串最少删除多少个字母就能变成只由字典内的单词组成的字符串 Sa ...

  6. The Cow Lexicon(dp)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7290   Accepted: 3409 Description Few k ...

  7. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

  8. POJ 3267:The Cow Lexicon 字符串匹配dp

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 4228 D ...

  9. BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典

    题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 401  Solv ...

随机推荐

  1. 在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字

    在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字呢? 正确的步骤如下: 1, 打开cmd命令行窗口 2, 输入命令 >chcp 65001 数字65001代表的是c ...

  2. f2fs源码解析(五) node管理结构梳理

    node是f2fs重要的管理结构, 它非常重要! 系统挂载完毕后, 会有一个f2fs_nm_info结构的node管理器来管理node的分配. f2fs_nm_info中最让人疑惑的是几颗基数树: s ...

  3. Node.js之Promise

    2015年发布了ES6标准,所谓 Promise,就是ES6标准的一个对象,用来传递异步操作的消息.它代表了某个未来才会知道结果的事件(通常是一个异步操作),并且这个事件提供统一的 API,可供进一步 ...

  4. [转]Linux查看物理CPU个数、核数、逻辑CPU个数

    From : http://www.cnblogs.com/emanlee/p/3587571.html # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个 ...

  5. c# nullable类型有什么用

    可空类型,语法: ;            int? inully = 10; Nullable<int> inullx0 = null;            int? inully0 ...

  6. 模拟alert和confirm

    啥也不说,先上图,有图有真相 :) 现在绝大多数网站都不用自带的alert和confirm了,因为界面太生硬了.因此这个插件就这样产生了... 来看插件的实现代码吧: (function () { $ ...

  7. JS 之继承

    ECMAScript继承是通过原型链来继承的.基本思想是利用原型来让一个引用类型继承另一个引用类型的属性和方法,使原型变为另一个对象的实例.通过原型链实现继承时,不能使用对象字面量创建原型方法,避免重 ...

  8. Andorid-15k+的面试题。

    andorid开发也做了3年有余了,也面试很多加企业,借此机会分享一下,我们中遇到过的问题以及解决方案吧,希望能够对正在找工作的andoird程序员有一定的帮助. 特别献上整理过的50道面试题目 1. ...

  9. 变量监控 指令 gt-wach

    index8.html <html><head> <title>变量监控指令 gt-watch</title> <script src=" ...

  10. Android数据共享

    Android数据共享 在Android应用程序开发的过程中,借助Bundle类对象来传递数据并不是在所有场景下都适用,就那简单的Intent类对象来说,就不能put进Bundle类对象中.当然不能否 ...