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. WebResource-asp.net自定义控件引用外部资源方法

    rom:http://www.lmwlove.com/ac/ID879 在asp.net中开发自定义控件时,如果我们要用到图片,外部css,js等文件,那么最好的方式就是将这些文件作为自定义控件嵌入的 ...

  2. XML CDATA的作用

    操作XML文件时,如果允许用户输入内容,例如∶"< ".">"."/".""等,当生成XML时,会破坏了XM ...

  3. C# log4net 不输出日志

    一个新项目,直接用了一些之前的代码,突然跟踪不到日志了.检查发现了原因,特在此记录. log4net的配置文件log4net_config.xml <?xml version="1.0 ...

  4. 模拟alert和confirm

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

  5. Listview实现不同类型的布局

    打开各种客户端发现 Listview的布局多种多样,在我以前的认知中listview不是只能放一种item布局嘛,我就震惊了,现在我自己的项目上要用到这种方式那么就去做下 原理是listview 的a ...

  6. [vim]vim 在win下乱码解决

    vim在win下遇到汉字乱码早就知晓,本以为通过如下设置即可解决乱码问题 set encoding=utf-8 set fileencoding=utf-8,chinese 这样设置是可以解决源码文件 ...

  7. Ubuntu下类似于Total Commander的两个工具

    Total Commander for linux Is there a Linux version? Unfortunately not. Because of problems with port ...

  8. Self Organizing Maps (SOM): 一种基于神经网络的聚类算法

    自组织映射神经网络, 即Self Organizing Maps (SOM), 可以对数据进行无监督学习聚类.它的思想很简单,本质上是一种只有输入层--隐藏层的神经网络.隐藏层中的一个节点代表一个需要 ...

  9. Chrome扩展开发之一——Chrome扩展的文件结构

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  10. 怎样写 OpenStack Neutron 的 Extension (四)

    上文说到需要在 /neutronclient/v2_0/myextension/extension.py 中分别定义五个 class:List/Show/Create/Delete/UpdateExt ...