poj 3267 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 2: L characters (followed by a newline, of course): the received message
Lines 3..W+2: The cows' dictionary, one word per line
Output
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 (动态规划)的更多相关文章
- POJ 3267 The Cow Lexicon
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ - 3267 The Cow Lexicon(动态规划)
https://vjudge.net/problem/POJ-3267 题意 给一个长度为L的字符串,以及有W个单词的词典.问最少需要从主串中删除几个字母,使其可以由词典的单词组成. 分析 状态设置很 ...
- poj 3267 The Cow Lexicon(dp)
题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...
- POJ 3267 The Cow Lexicon 简单DP
题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...
- PKU 3267 The Cow Lexicon(动态规划)
题目大意:给定一个字符串和一本字典,问至少需要删除多少个字符才能匹配到字典中的单词序列.PS:是单词序列,而不是一个单词 思路: ...
- POJ3267——The Cow Lexicon(动态规划)
The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...
- POJ 3267:The Cow Lexicon(DP)
http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
- POJ 3267:The Cow Lexicon 字符串匹配dp
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 4228 D ...
- POJ 3267-The Cow Lexicon(DP)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8252 Accepted: 3888 D ...
随机推荐
- sublime -text 删除已安装插件
按ctr+shift +p然后输入remove 回车,再输入要删除的插件名
- Human Gene Functions(dp)
http://poj.org/problem?id=1080 #include <stdio.h> #include <stdlib.h> #include <strin ...
- bzoj3436: 小K的农场(差分约束)
3436: 小K的农场 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1575 Solved: 690[Submit][Status][Discus ...
- sklearn中的数据预处理和特征工程
小伙伴们大家好~o( ̄▽ ̄)ブ,沉寂了这么久我又出来啦,这次先不翻译优质的文章了,这次我们回到Python中的机器学习,看一下Sklearn中的数据预处理和特征工程,老规矩还是先强调一下我的开发环境是 ...
- windows系统下nodejs安装、环境配置及删除NPM全局配置
nodejs安装及设置NPM全局路径 删除NPM全局路径配置 一.nodejs安装及设置NPM全局路径 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...
- 关于offer对比
前天签了三方,在签约前的几个小时,还在纠结到底该accept哪个offer,相信很多同学都会遇到这个问题,就由此展开去吧. 关于offer的选择,无外乎以下几个考察点:1.个人发展:2.地域:3.薪资 ...
- ROS-TF-新建坐标系
前言:在前面的试验中,我们分别有wolrd,turtle1和turtle2三个坐标系,并且world是turtle1和turtle2的父坐标系.现在我们来新建一个自定义坐标系,让turtle2跟着新的 ...
- Dotnet Core2.1 使用CodeFirst
一.添加Mysql引用 二.添加连接字符串 三.配置startup.cs 三.初始化数据库 Add-Migration init Update-Database 四.数据迁移 user实体添加了pas ...
- Yoga710笔记本Win10和Ubuntu系统共存
联想yoga710默认安装了win10系统,且使用EFI分区格式,安装Ubuntu不是一般的困难,经公司小哥的帮助下,几次终于完成了Ubuntu和Win10 共存. 经过多次安装测试,暂时能运行成功的 ...
- MAMP PRO mysql无法启动
mac上可能勾选了软件自动更新,然后MAMP PRO 升级了. 升级了之后,mysql启动就出问题了,看报错日志: InnoDB: The error means the system cannot ...