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

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

dp[i]代表以i为结尾的最优选择,状态转移方程为:

dp[i]=min(dp[i-1]+1,opt); opt:包括第 i个字符的最优选择;

把原先的字典里的词组反转,枚举字典里的全部词汇,找到最优解。

#include<stdio.h>
#include<queue>
#include<map>
#include<string>
#include<string.h>
using namespace std;
#define N 305
const int inf=0x1f1f1f1f;
char str[N*2][30],s[N];
int dp[N];
int main()
{
int i,j,k,n,m;
char ch;
while(scanf("%d%d",&m,&n)!=-1)
{
scanf("%s",s+1);
for(i=0;i<m;i++)
{
scanf("%s",str[i]);
int len=strlen(str[i]); //字符串反转
for(j=0;j<len/2;j++)
{
ch=str[i][j];
str[i][j]=str[i][len-j-1];
str[i][len-1-j]=ch;
}
str[i][len]='\0';
}
dp[0]=0;
int tmp;
for(i=1;i<=n;i++)
{
tmp=dp[i-1]+1; //tmp初始化为该字符舍去时的值
for(j=0;j<m;j++)
{
if(str[j][0]!=s[i])
continue;
int l=1,len=strlen(str[j]);
for(k=i-1;k>0&&l<len;k--) //寻找该单词出现的最早位置
{
if(s[k]==str[j][l])
l++;
}
if(l==len) //包括此单词
tmp=min(tmp,dp[k]+(i-k-len));
}
dp[i]=tmp;
}
printf("%d\n",dp[n]);
}
return 0;
}

poj 3267 The Cow Lexicon (动态规划)的更多相关文章

  1. POJ 3267 The Cow Lexicon

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

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

    https://vjudge.net/problem/POJ-3267 题意 给一个长度为L的字符串,以及有W个单词的词典.问最少需要从主串中删除几个字母,使其可以由词典的单词组成. 分析 状态设置很 ...

  3. poj 3267 The Cow Lexicon(dp)

    题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...

  4. POJ 3267 The Cow Lexicon 简单DP

    题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...

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

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

  6. POJ3267——The Cow Lexicon(动态规划)

    The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...

  7. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

  8. POJ 3267:The Cow Lexicon 字符串匹配dp

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

  9. POJ 3267-The Cow Lexicon(DP)

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

随机推荐

  1. 通过DOM实现点击隐藏父元素

    HTML代码简单如下: <ul id='ul1'> <li><a href="javascript:">1</a></li&g ...

  2. springboot+webmagic实现java爬虫jdbc及mysql

    前段时间需要爬取网页上的信息,自己对于爬虫没有任何了解,就了解了一下webmagic,写了个简单的爬虫. 一.首先介绍一下webmagic: webmagic采用完全模块化的设计,功能覆盖整个爬虫的生 ...

  3. prim解决最小生成树问题

    #include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> ...

  4. windows 装XP系统

    笔记本型号:HPCQ40-506AX 1.在BIOS中更改启动顺序:将USB设为第一启动项2.插入装有PE系统的USB设备3.开机后一直按F124.到达选择系统界面,目前我的HPCQ40用其他系统进去 ...

  5. JDBC的详细使用

    1.首先说一下需要用到的工具: ①我这里用的数据库是MySql5.6 ,MySql6.0开始被Oracle收购需要付费了,6.0以下版本免费. ②去Maven仓库下载JDBC的jar包 Maven仓库 ...

  6. AI.框架理论.语义网.语言间距.孤单

    刷个博客,转载自于科学网:AI.框架理论.语义网.语言间距.孤单 一:引言: AI几乎是计算机科学家的梦想,自动化比计算机发展的要早的多.早期的自动化节省了大量人力,激发了人类懒惰的滋长和对自身进化缓 ...

  7. C++程序设计实验安排

    2016-2017第二学期C++程序设计的实验时间与地点安排如下表,请大家根据时间按时来上机实验.另外,因为原来安排在4.1的实验因为调休补周一的课,因此挪至周五.另外第4次周六的课,考虑有一些同学有 ...

  8. Maven常见异常及解决方法

    异常1: [ERROR] Failed to execute goal on project biz_zhuhai: Could not resolve dependencies for projec ...

  9. js开发性能(一)

    随着js技术的发展,性能问题开始被越来越多的人关注,最近了解了一些关于前端性能的问题,这里主要讨论一下在js脚本加载和执行的过程中,我们应该怎么样来提高js的性能. js脚本的处理 初学前端的时候,我 ...

  10. windows10上安装mysql详细图文教程

    在windows10上安装mysql详细图文教程   这篇文章主要介绍了在windows10上安装mysql详细图文教程,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起看看吧 环境:windw ...