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. iOS社会化分享(干货)

    一.苹果原生集成的社会化分享 1.哪些平台 (1)Twitter (2)FaceBook (3)Flickr (4)Vimeo (5)新浪微博  :iOS6 (6)腾讯微博 : iOS7 2.框架 : ...

  2. 95.Extjs 表单中自定义的验证规则 VTypes

    1 Ext.onReady(function(){ Ext.QuickTips.init(); //重写 (自定义)xtype Ext.apply(Ext.form.VTypes,{ repetiti ...

  3. windows系统下nodejs安装、环境配置及删除NPM全局配置

    nodejs安装及设置NPM全局路径 删除NPM全局路径配置 一.nodejs安装及设置NPM全局路径 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...

  4. 5.13会话技术Cookie---Session

    .会话技术: 1.会话:一次会话中包含多次请求和相应. 一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止 2.功能:在一次会话的范围内的多次请求间,共享数据 3.方式: 1.客 ...

  5. Hadoop2.6.5高可用集群搭建

    软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3, m4, m5这五部机, 每部主机的用户名 ...

  6. ANN:DNN结构演进History—LSTM网络

    为了保持文章系列的连贯性,参考这个文章: DNN结构演进History-LSTM_NN 对于LSTM的使用:谷歌语音转录背后的神经网络 摘要: LSTM使用一个控制门控制参数是否进行梯度计算,以此避免 ...

  7. eclipse快捷键:

    打开快捷键提示: ctrl + shift + L; 自动补全代码: Alt + /; 快速修复: ctrl + 1; 导包: ctrl + shift + o; 格式化代码: ctrl + shif ...

  8. 【sqli-labs】 less25a GET- Blind based -All you OR&AND belong to us -Intiger based(GET型基于盲注的去除了or和and的整型注入)

    因为过滤是针对输入的字符串进行的过滤,所以如果过滤了or and的话,提交id=1和id=and1结果应该相同 http://localhost/sqli-labs-master/Less-25a/? ...

  9. PAT_A1134#Vertex Cover

    Source: PAT A1134 Vertex Cover (25 分) Description: A vertex cover of a graph is a set of vertices su ...

  10. PAT_A1076#Forwards on Weibo

    Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...