POJ3267 The Cow Lexicon(DP+删词)
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 2: L characters (followed by a newline, of
course): the received message
Lines 3..W+2: The cows' dictionary, one
word per line
Output
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+删词)的更多相关文章
- POJ3267 The Cow Lexicon(dp)
题目链接. 分析: dp[i]表示母串从第i位起始的后缀所对应的最少去掉字母数. dp[i] = min(dp[i+res]+res-strlen(pa[j])); 其中res 为从第 i 位开始匹配 ...
- POJ3267——The Cow Lexicon(动态规划)
The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...
- poj3267--The Cow Lexicon(dp:字符串组合)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3864 D ...
- POJ 3267-The Cow Lexicon(DP)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8252 Accepted: 3888 D ...
- USACO 2007 February Silver The Cow Lexicon /// DP oj24258
题目大意: 输入w,l: w是接下来的字典内的单词个数,l为目标字符串长度 输入目标字符串 接下来w行,输入字典内的各个单词 输出目标字符串最少删除多少个字母就能变成只由字典内的单词组成的字符串 Sa ...
- The Cow Lexicon(dp)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7290 Accepted: 3409 Description Few k ...
- POJ 3267:The Cow Lexicon(DP)
http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
- POJ 3267:The Cow Lexicon 字符串匹配dp
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 4228 D ...
- BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 401 Solv ...
随机推荐
- Entity Framework版本历史概览
转自:http://www.cnblogs.com/fecktty2013/archive/2014/09/26/entityframework-overview.html EF版本 .net fra ...
- C语言 数组做函数参数不传数组个数的遍历方法
//数组做函数参数不传数组个数的遍历方法 #include<stdio.h> #include<stdlib.h> #include<string.h> void ...
- easyui-combobox的取值问题
例子:<select id="cc" class="easyui-combobox" name="cc" style="wi ...
- ASP.NET Web API路由规则(二)
默认的规则 在ASP.NET MVC4中 global.asax.cs代码中并无注册默认路由规则的代码 代码如下: public class WebApiApplication : System.We ...
- caffe windows 学习第一步:编译和安装(vs2012+win 64)
没有GPU,没有linux, 只好装caffe的windows版本了. 我的系统是win10(64位),vs 2012版本,其它什么都没有装,因此会需要一切的依赖库. 其实操作系统只要是64位就行了, ...
- opencv3学习:reshape函数
在opencv中,reshape函数比较有意思,它既可以改变矩阵的通道数,又可以对矩阵元素进行序列化,非常有用的一个函数. 函数原型: C++: Mat Mat::reshape() const 参数 ...
- HoloLens开发手记 - Unity之Spatial Sounds 空间声音
本文主要讲述如何在项目中使用空间声音特性.我们主要讲述必须的插件组件和Unity声音组件和属性的设置来确保空间声音的实现. Enabling Spatial Sound in Unity 在Unity ...
- 解决The current branch is not configured for pull No value for key branch.master.merge found in confi
1.在本地工程目录找到config文件(我的是在E:\rocket\rocket\.git): 2.修改config文件内容为: [core] repositoryformatversion = fi ...
- web压测工具http_load原理分析
一.前言 http_load是一款测试web服务器性能的开源工具,从下面的网址可以下载到最新版本的http_load: http://www.acme.com/software/http_load/ ...
- 约瑟夫环的java解决
总共3中解决方法,1.数学推导,2.使用ArrayList递归解决,3.使用首位相连的LinkedList解决 import java.util.ArrayList; /** * 约瑟夫环问题 * 需 ...