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 ...
随机推荐
- bzoj 2599(点分治)
2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 3642 Solved: 1081[Submit][Statu ...
- Elasticsearch 7.1.1 集群 + 配置身份验证
一.安装Elasticsearch 1.1 环境说明 Centos7.6 Elasticsearch7.1.1 #挂载数据盘 fdisk /dev/vdb n,p,,回车,回车,wq fdisk -l ...
- android 可拖动控件 ontouchevent
首先附上文章的转载内容的链接: 学习android 可拖动事件首先需要对android的屏幕和touchevent参数建立一个详细的知识结构. 1.android坐标系统 一.首先明确一下 andro ...
- Codeforces 609D 被二分教做人
传送门:http://codeforces.com/problemset/problem/609/D (如需转载,请注明出处,谢谢O(∩_∩)O) 题意: Nura想买k个小玩意,她手上有 s 个bu ...
- G - Wrong Subtraction
Problem description Little girl Tanya is learning how to decrease a number by one, but she does it w ...
- SDOI 2018 round2游记
Day 0 早上起来从北京到济南 住宿环境不错 不过比赛环境怎么这么low啊 而且我在最偏僻的考场中最偏僻的角落里 身边居然是个妹子?! Day1 7:40到的考试地点 发现诸位大佬已经打完板子了or ...
- SQL server高级语法
1. 公共表达式CTE 公用表表达式 (CTE) 具有一个重要的优点,那就是能够引用其自身,从而创建递归 CTE.递归 CTE 是一个重复执行初始 CTE 以返回数据子集直到获取完整结果集的公用表表达 ...
- ★Java语法(五)——————————三元运算符
package 课上练习; public class 三元运算符 { //用法: 数据类型 变量 = 布尔表达式? 条件满足设置内容:条件不满足设置内容 : public static void ma ...
- 【sqli-labs】 less40 GET -Blind based -String -Stacked(GET型基于盲注的堆叠查询字符型注入)
提交,页面正常,说明是')闭合的 http://192.168.136.128/sqli-labs-master/Less-40/?id=1')%23 http://192.168.136.128/s ...
- python tips:类的绑定方法(bound)和非绑定方法(unbound)
类属性只有类及其实例能够访问,可以理解为一个独立的命名空间. Python中类属性的引用方式有两种: 1. 通过类的实例进行属性引用,称为绑定方法(bound method),可以理解为方法与实例绑定 ...