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. Eclipse下使用Fat Jar插件对源代码进行打包

    这两天需要对一个项目进行打包,并在服务器上部署成后台服务模式进行执行,原来使用eclipse进行打包很难用,配置文件容易出错,生成的jar不能正常运行.后来发现Fat Jar Eclipse Plug ...

  2. spring xml 空模板-applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  3. codevs1024一塔湖图(丧心病狂的建图)

    /* 丧心病狂的最短路 关键是建图 根据题目中给的路 拆出节点来 建图 (i,j) -->(j-1)*n+i 然后根据障碍 把死路 湖覆盖的dis改变成极大值 然后Floyd 然后 然后就没有然 ...

  4. 导出到excel

    /// <summary> /// 导出 /// </summary> /// <param name="table">数据表</para ...

  5. WPF 媒体播放器(MediaElement)使用实例(转)

    在WPF 中可以使用MediaElement 为应用程序添加媒体播放控件,以完成播放音频.视频功能.由于MediaElement 属于UIElement,所以它同时也支持鼠标及键盘的操作.本篇将使用M ...

  6. C#内存修改

    先通过 System.Diagnostics.Process类获取想要编辑的进程 调用API [Flags]                    public enum ProcessAccessT ...

  7. Fluent NHibernate之旅

    Fluent NHibernate 之旅 导航篇: [原创]Fluent NHibernate之旅开篇: [原创]Fluent NHibernate之旅二--Entity Mapping: [原创]F ...

  8. iOS里面消除使用代理调用方法时间警告问题

    iOS里面有三种调用函数的方式: 直接调用方法   [对象名 方法]; performselector:    [对象名 perform方法]; NSInvocation     调用 在使用代理调用 ...

  9. Javascript的AMD规范

    Javascript发展到今天,已经从一个小丑语言变成了不可替代的前端利器,已经脱离了低端的玩笑脚步,而转变为有规可依的强大语言. 本文主要讲述下如今被大力推广的AMD规范,为什么要AMD,什么场景是 ...

  10. 【转】 C语言自增自减运算符深入剖析

    转自:http://bbs.csdn.net/topics/330189207 C语言的自增++,自减--运算符对于初学者来说一直都是个难题,甚至很多老手也会产生困惑,最近我在网上看到一个问题:#in ...