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. 软件工程实践小队--团队项目NABC

    团队项目的NABC 1) N (Need 需求) 作为一个网上教学问答系统,用户的基本需求很明确,即为:提问.搜索.浏览.回答.编辑.评论.附加需求还有: 获取金币.提升等级. 提问:关于一门学科,用 ...

  2. Spring MVC 入门教程示例 (一)

    今天和大家分享下  Spring MVC  入门教程 首先还是从 HelloWorld  web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...

  3. Codeforces Round #281 (Div. 2)

    题目链接:http://codeforces.com/contest/493 A. Vasya and Football Vasya has started watching football gam ...

  4. 【DP】permu

    permu [Description] 给定两个1~N的全排列A,B.有两个指针q和p,一开始q.p都为0,可执行以下三种操作: 1.q+1:2.p+1:3.q+1且p+1(Aq+1≠Bp+1时才可以 ...

  5. 剑指offer--7题

    *题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. *句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. *例如输入“I am a student.”,则输出“st ...

  6. delete错误

    今天找了半天delete错误,后来才知道是MTd和MDd模式的问题,MTd的内存申请和释放必须在同一个模块里面,接口上面不能使用stl等,MDd可以使用.改成MDd就可以了

  7. jquery 常用组件的小代码

    获得所有复选框的值 function getAllValue() { var str=""; $("input[name='checkbox']:checkbox&quo ...

  8. 研究AFIncrementalStore

    一.增量存贮(AFIncrementalStore:NSIncrementalStore)在CoreData中所处位置

  9. 安装JDK后环境变量的配置

    1.JAVA_HOME:JDK的安装路径2.CLASSPATH:java运行时加载的类路径,即JDK的lib下面的tools.jar和dt.jar3.PATH:JDK安装包下的bin目录

  10. 利用Linq + Jquery + Ajax 异步分页的实现

    在Web显示的时候我们经常会遇到分页显示,而网上的分页方法甚多,但都太过于消耗带宽,所以我想到了用Ajax来分页,利用返回的Json来处理返回的数据, 大大简化了带宽的压力. 先说下思路,无非就是异步 ...