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 ...
随机推荐
- 实现泛型IEnumerable接口
用C#实现一个类的IEnumerable接口时有两种方法:1)实现非泛型IEnumerable接口:2)实现泛型IEnumerable(T)接口.如果采用方法1,当集合元素T是值类型时,将涉及到巨多的 ...
- 明解C语言
本文为阅读书籍<明解C语言-中级篇>所积累的知识点及编译书本代码时遇到的问题.部分对应代码在\Code_2018\BK_明解C语言目录下.每个代码内都含有程序功能.思路.疑惑点等内容,如有 ...
- ZOJ2477 Magic Cube
题目: This is a very popular game for children. In this game, there's a cube, which consists of 3 * 3 ...
- BZOJ 2333 左偏树 (写得我人生都崩溃了...)
思路: 高一神犇 竟然 问我这道题 我光荣地 看着题解(划掉) 写了一下午 QaQ multiset不能erase(一个值) 这样就把等于这个值 的数都erase掉了 (woc我一开始不 ...
- Safe Area Layout Guide before iOS 9.0
今天使用Xcode9.1重建项目,什么都没写运行报错:Safe Area Layout Guide before iOS 9.0!目前为止,不晓得原因,现记录解决方法:
- 前端-git思维导图笔记
命令汇总 git config配置本地仓库 常用git config --global user.name.git config --global user.email git config --li ...
- python练习--1、简易登录接口
function:用于实现简单的用户登录接口. description:用户信息存储于字典中,增加用户需手动写入代码中.最多可以尝试登录9次,首次使用会先在程序目录下创建锁文件.若在用户栏输入quit ...
- 酷派改变者S1(C105/C105-6/C105-8) 解锁BootLoader 并刷入recovery root
首先下载好工具链接:https://pan.baidu.com/s/1qZjOCUw 密码:u2dr 备用下载链接:https://pan.baidu.com/s/1pMlmAef 本篇教程教你如何傻 ...
- LinkedList 源码
1.类继承结构 结构: 2.成员及方法 注意:其中 getFirst,getLast,removeFirst,removeLast,el ...
- 三维重建:SLAM的粒度和工程化问题
百度百科的定义.此文引用了其他博客的一些图像,如有侵权,邮件联系删除. 申明一下,SLAM不是一个算法,而是一个工程. 在计算机视觉中, 三维重建是指根据单视图或者多视图的图像重建三维信息的过程. 由 ...