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. 搭建SpringMVC+MyBatis开发框架四

    在src/main下面新建一个resouces文件夹,我们继续配置一些资源 1.新增applicationContext.xml:  <?xml version="1.0" ...

  2. php编写验证码

    今天学习到了php登录时的验证码,验证码在我们平时的网站建设中是非常重要的,对于放置一些灌水机.脚本攻击是一个很好地策略. 下面是我写的代码: <?php session_start(); // ...

  3. Python中的List,Tuple,Dic,Set

    Python中的List,Tuple,Dic,Set List定义 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 ...

  4. 《我是IT小小鸟》读书笔记

    转眼间,大学的第二学期悄悄来临了,老师给我们布置了一道原本我以为很无趣的题目----写<我是IT的读书笔记>,但是我读了<我是IT小小鸟>这本书后,令我受益匪浅:五个人,每个人 ...

  5. SqlServer 临时表、表变量、函数 替代游标

    http://www.cnblogs.com/chongzi/archive/2011/01/19/1939106.html 临时表 存放在tempdb中 --存储过程中将多表连接结果写入到临时表中, ...

  6. Leetcode#97 Interleaving String

    原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...

  7. GS连接事件

    GS网络连接事件 //网络事件 //这个事件是在libevent里面的收到的事件就是在那个listen里面,就是客户端打开,服务器收到通知 link_stat stat = (link_stat)rP ...

  8. Codeforces Round #204 (Div. 2)->D. Jeff and Furik

    D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Ubuntu下配置Docbook环境

    1.准备环境 $sudo apt-get install xsltproc $sudo apt-get install docbook-xsl $sudo apt-get install docboo ...

  10. win7/8下VirtualBox虚拟Ubuntu共享文件夹设置

    实验环境: 主机:win8.1 虚拟机软件:VirtualBox4.3 虚拟的主机:centos6.5 final 亲测可用! 1. 安装增强功能包(VBoxGuestAdditions)  打开虚拟 ...