POJ3267——The Cow Lexicon(动态规划)
The Cow Lexicon
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
题目大意:
题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列.
PS:是匹配单词序列,而不是一个单词
解题思路:
动态规划。网上多数是从后往前推。我从前往后推也AC了。发现更好理解一些。
具体细节看代码。
Code:
- /*************************************************************************
- > File Name: poj3267.cpp
- > Author: Enumz
- > Mail: 369372123@qq.com
- > Created Time: 2014年10月17日 星期五 18时28分18秒
- ************************************************************************/
- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- #include<string>
- #include<cstring>
- #include<list>
- #include<queue>
- #include<stack>
- #include<map>
- #include<set>
- #include<algorithm>
- #define MAXN 1001
- using namespace std;
- char dic[MAXN][MAXN];
- char mes[MAXN];
- int dp[MAXN];
- int N,M;
- int Incl(int i,int j) //判断字典j是否包含在字符串前i位内
- {
- int Ndic=strlen(dic[j])-;
- if (dic[j][Ndic]!=mes[i]) return -;
- while ()
- {
- if (Ndic==-||i==-) break;
- if (dic[j][Ndic]==mes[i]) i--,Ndic--;
- else i--;
- }
- if (Ndic==-) return i+; //返回字典i匹配的第一个字符的位置。
- else return -;
- }
- int solve()
- {
- for (int i=; i<=M-; i++)
- {
- if (i!=)
- dp[i]=dp[i-]+;
- else
- dp[i]=;
- for (int j=; j<=N; j++)
- {
- int k=Incl(i,j); //判断字典j是否包含在字符串前i位内
- if (k!=-)
- {
- //回溯到字符串开头处时,需要特判。注意if语句!WA了好几遍
- if (k-<&&dp[i]>i+-k-strlen(dic[j]))
- dp[i]=(i+-k-strlen(dic[j]));
- else if (dp[i]>dp[k-]+i+-k-strlen(dic[j]))
- dp[i]=dp[k-]+i+-k-strlen(dic[j]);
- }
- }
- }
- return dp[M-];
- }
- int main()
- {
- while (scanf("%d%d",&N,&M)!=EOF)
- {
- memset(dp,,sizeof(dp));
- memset(dic,,sizeof(dic));
- memset(mes,,sizeof(mes));
- scanf("%s",mes);
- for (int i=; i<=N; i++)
- scanf("%s",dic[i]);
- printf("%d\n",solve());
- }
- return ;
- }
POJ3267——The Cow Lexicon(动态规划)的更多相关文章
- POJ3267 The Cow Lexicon(DP+删词)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9041 Accepted: 4293 D ...
- poj 3267 The Cow Lexicon (动态规划)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8167 Accepted: 3845 D ...
- PKU 3267 The Cow Lexicon(动态规划)
题目大意:给定一个字符串和一本字典,问至少需要删除多少个字符才能匹配到字典中的单词序列.PS:是单词序列,而不是一个单词 思路: ...
- POJ3267 The Cow Lexicon(dp)
题目链接. 分析: dp[i]表示母串从第i位起始的后缀所对应的最少去掉字母数. dp[i] = min(dp[i+res]+res-strlen(pa[j])); 其中res 为从第 i 位开始匹配 ...
- POJ 3267 The Cow Lexicon
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3267:The Cow Lexicon(DP)
http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
- The Cow Lexicon
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...
- HDOJ-三部曲-1015-The Cow Lexicon
The Cow Lexicon Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- poj3267--The Cow Lexicon(dp:字符串组合)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3864 D ...
随机推荐
- MSSQL Transaction[事务] and Procedure[存储过程]
--事务分三种 --1.显示事务 --我们手动begin transaction ...... commit transaction/rollback transaction --上面这种写法叫做“显 ...
- Notes of the scrum meeting(11/3)
meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪 ...
- MFC源码不能设置断点调试
用VS2013中MFC开发应用程序时,进入MFC源码设置断点调试.但是在调试过程中发现无法进入源码.最后发现在MFC设置的MFC的使用默认值是在共享dll中使用MFC,这就意味着MFC中的源码并没有连 ...
- Mac下安装Tomcat及配置
今天介绍Mac下Tomcat的安装及配置: 1.在搜索引擎(如:必应或百度)中搜索“Tomcat”,第一条搜索结果就是Tomcat官方地址: 2.在左侧选择“Tomcat8”或“Tomcat9”,我这 ...
- PE文件结构深入详解
一.PE结构基础 看了很多PE结构类的东东,要不上来就是整体结构,要不就是一大堆ASM代码,看的我等菜鸟有点难受!所以自己写个帖·学习PE我们先来弄懂几个问题! 1:几个地址的概念 VA:虚拟地址,也 ...
- post 方式提交XML文件调用接口
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java. ...
- YAPF:Google开源的Python代码格式化工具
点这里 现在的大多数 Python 代码格式化工具(比如:autopep8 和 pep8ify)是可以移除代码中的 lint 错误.这显然有些局限性.比如:遵循 PEP 8 指导的代码可能就不会被格式 ...
- DB2对年份的处理Year()
public DataSet GetCustomerAllocListByQC(CustomerAllocQueryDataContract aQC) { StringBuilder sql = ne ...
- zoj 2686 Cycle Game 博弈论
其实规律很好找的,当从某点开始,向某一边找出非0的个数,为奇数时必胜. 代码如下: #include<iostream> #include<cstdio> using name ...
- (转)android ListView详解
转自: http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html 在android开发中ListView是比较常用的组件,它以列表的形 ...