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 ...
随机推荐
- Excel下用SQL语句实现AVEDEV函数功能
Excel下AVEDEV函数返回一组数据点到其算术平均值的绝对偏差的平均值. AVEDEV 是对一组数据中变化性的度量.最常见的应用就是统计平均分差. 但是如果在Excel中写SQL进行一些复杂的统计 ...
- C#委托详解(3):委托的实现方式大全(续)
接上篇(C#委托详解(2):实现方式大全),本篇继续介绍委托的实现方式. 4.Action<T>和Func<T>委托 使用委托时,除了为每个参数和返回类型定义一个新委托类型之外 ...
- 响应式设计Responsinator工具推荐
from:http://www.25xt.com/allcode/4066.html 原文推荐了5种,感觉有用的吧就这一种,所以收藏过来. Responsinator工具的好处Responsinato ...
- NYOJ-214 单调递增子序列(二) TLE 分类: NYOJ 2014-01-28 22:57 171人阅读 评论(0) 收藏
#include<stdio.h> #include<stdlib.h> #define max(x,y) x>y?x:y #define MAXX 100005 int ...
- map线程
来看看map线程到底是如何运行的 很早就知道一个map是一个线程,以后有可能改成一个map一个进程,那就先来看看一个map一个线程是如何运作的 其实刚开始整个服务器就是两个线程,但发现这样服务器支持的 ...
- 使用Provider时提示:Unable to get provider...
具体原因还不清楚,只是找到了原因: 这是因为自定义的Provider放的包路径不对,自定义的Provider应该放到和MainActivity同一个包中.
- Hibernate SQL方言 (hibernate.dialect)
数据库 hibernate方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect ...
- Asp.net MVC 实现图片上传剪切
使用技术:Asp.net MVC与jquery.uploadify,Jcrop 首先上页面 01 <strong><!DOCTYPE html> 02 <html> ...
- POJ 1548 Robots (Dilworth)
Robots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3621 Accepted: 1643 Description Yo ...
- Linux Rsync
一.Rsync介绍 1.什么是Rsync Rsync 即Remote Rynchronization,是一款开源的.快速的.多功能的.可实现全量或增量的本地或者远程数据镜像同步复制.备份的优秀工具. ...