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(动态规划)的更多相关文章

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

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

  2. poj 3267 The Cow Lexicon (动态规划)

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

  3. PKU 3267 The Cow Lexicon(动态规划)

    题目大意:给定一个字符串和一本字典,问至少需要删除多少个字符才能匹配到字典中的单词序列.PS:是单词序列,而不是一个单词 思路:                                     ...

  4. POJ3267 The Cow Lexicon(dp)

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

  5. POJ 3267 The Cow Lexicon

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

  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. The Cow Lexicon

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

  8. HDOJ-三部曲-1015-The Cow Lexicon

    The Cow Lexicon Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

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

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

随机推荐

  1. haproxy实现mysql从库负载均衡

    本文主要讲述通过haproxy实现mysql从库间的负载均衡,至于mysql主从的搭建,本文不再重述,可以参考我之前写的博客. 1.首先下载haproxy包 wget http://haproxy.1 ...

  2. mysql开机脚本

    #!/bin/bash basedir=/home/app/db/mysql datadir=$basedir/data conf=$basedir/etc/my.cnf pid_file=$data ...

  3. Java应用程序实现屏幕的"拍照"

    有时候,在Java应用程序开发中,如:远程监控或远程教学,常常需要对计算机的屏幕进行截取,由于屏幕截取是比较接近操作系统的操作,在Windows操作系统下,该操作几乎成了VC.VB等的专利,事实上,使 ...

  4. 【Same Tree】cpp

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  5. RobotFramework-调用.py文件

    RobotFramework-调用.py文件,直接运行: 注意:文件路径的\全部换成好了/

  6. ionice

    ionice – 获取或设置程序的IO调度与优先级. 命令格式: ionice [[-c class] [-n classdata] [-t]] -p PID [PID]… ionice [-c cl ...

  7. 【BZOJ】【1912】【APIO2010】patrol巡逻

    树形DP 说是树形DP,其实就是求树的最长链嘛…… K=1的时候明显是将树的最长链的两端连起来最优. 但是K=2的时候怎么搞? 考虑第一次找完树的最长链以后的影响:第一次找过的边如果第二次再走,对答案 ...

  8. 【POJ】【3680】Intervals

    网络流/费用流 引用下题解: lyd: 首先把区间端点离散化,设原来的数值i离散化后的标号是c[i].这样离散化之后,整个数轴被分成了一段段小区间. 1.建立S和T,从S到离散化后的第一个点连容量K, ...

  9. [noip2005提高]过河 dp

    由于L的范围到了109,用普通dp做肯定是不成了: 可以观察到M的数量很小,dp在转移的过程中有大量的无用转移: 可以想到压缩范围,问题是如何压缩,观察若S=9,T=10时,能到达的点,9,10,18 ...

  10. 一个包的net到gs流程

    再来看看一个包走共享内存的流程 先来看看net进程这块如何处理的 {//用shareData这种类型封装刚才从无锁队列中取到的包 shareData sd; sd.channel_id = pkt.c ...