Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7290   Accepted: 3409

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

题意:给出一个由m个字符组成的单词和n个单词表,问至少删除多少个字母使这个单词才能由下面的单词表中的单词组成;
browndcodw 中删除两个d后由brown 和 cow 组成;

不得不说dp题真心难,想了半天没思路,发现自己还是太弱了,BU童鞋给讲的思路;

思路:从最后一个字母开始匹配,先初始化,dp[m] = 0,假使当前字母不能匹配,则dp[i] = dp[i+1] + 1;然后,遍历单词表中每个单词,如果有单词的首字母与当前字母相同,那么该单词有可能和这个字母以后的单词匹配,经判断后若能匹配,就算出匹配成功要删除的字母个数,我的是(cur-i)-len[j],取dp[i] 和 dp[cur]+(cur-i)-len[j] 的较小者

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; char s[];//待匹配单词
char dict[][];//单词表
int dp[];// dp[i]表示到第i个字母为止要删除的最少字母数;
int len[];//保存单词表中每个单词的长度;
int n,m; int main()
{
int i,j;
scanf("%d %d",&n,&m);
scanf("%s",s);
for(i = ; i < n; i++)
{
scanf("%s",dict[i]);
len[i] = strlen(dict[i]);
} dp[m] = ;//初始化 for(i = m-; i >= ; i--)
{
dp[i] = dp[i+] + ;//初始化 for(j = ; j < n; j++)//遍历每个单词
{
if(dict[j][] == s[i] && (m-i) >= len[j])//若有个单词的首字母与s[i]相同
{
int cur = i+, cnt = ; while(cur < m && dict[j][cnt])
{
if(s[cur++] == dict[j][cnt])
cnt++;
}//检查是否可以匹配 if(cnt == len[j])//若能匹配,取较小者
dp[i] = min(dp[i],dp[cur]+(cur-i)-len[j]);//(cur-i)-len[j]表示删除的字母个数
}
}
}
printf("%d\n",dp[]);
return ;
}
												

The Cow Lexicon(dp)的更多相关文章

  1. POJ3267 The Cow Lexicon(DP+删词)

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

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

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

  3. POJ 3267-The Cow Lexicon(DP)

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

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

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

  5. POJ3267 The Cow Lexicon(dp)

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

  6. POJ 3267:The Cow Lexicon(DP)

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

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

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

  8. POJ 3267 The Cow Lexicon

    又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...

  9. The Cow Lexicon

    The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...

随机推荐

  1. 一致性哈希(Consistent Hash)

    http://blog.csdn.net/cywosp/article/details/23397179/ http://www.codeproject.com/Articles/56138/Cons ...

  2. 第三篇:python高级之生成器&迭代器

    python高级之生成器&迭代器   python高级之生成器&迭代器 本机内容 概念梳理 容器 可迭代对象 迭代器 for循环内部实现 生成器 1.概念梳理 容器(container ...

  3. native跟volatile

    native是告知编译器 该方法是其他语言实现的 比如C 呵呵 private native void CoutSea();没有方法实现部分的 volatile是Java语言的关键字,用在变量的声明中 ...

  4. angularjs hover

    <ul class="pdl-15"><li ng-repeat="order in vm.selectOrders" ng-class=&q ...

  5. html multiple select option 分组

    普通html方式展示<select name="viewType" style="width: 100%;height: 300px;" multiple ...

  6. postgresql sql修改表,表字段

    1.更改表名 alter table 表名 rename to 新表名 2.更改字段名 alter table 表名 rename 字段名 to 新字段名 3.增加列 ALTER TABLE ud_w ...

  7. rdlc报表

    也是第一次接触报表这个东西.现在在我理解,报表无非就是两个内容,格式和数据. 格式没有多少了解,就记录了,以后再续.数据的绑定和结果的显示是怎么实现的呢? 今天的主角就是rdlc这个文件和Report ...

  8. 用java写bp神经网络(三)

    孔子曰,吾日三省吾身.我们如果跟程序打交道,除了一日三省吾身外,还要三日一省吾代码.看代码是否可以更简洁,更易懂,更容易扩展,更通用,算法是否可以再优化,结构是否可以再往上抽象.代码在不断的重构过程中 ...

  9. 【转】 iOS开发之手势gesture详解

    原文:http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html 前言 在iOS中,你可以使用系统内置的手势识别 (Gesture ...

  10. [转]Windows中的命令行提示符里的Start命令执行路径包含空格时的问题

    转自:http://www.x2009.net/articles/windows-command-line-prompt-start-path-space.html 当使用Windows 中的命令行提 ...