POJ 3267 The Cow Lexicon 简单DP
题目链接: http://poj.org/problem?id=3267
从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数。
状态转移方程为:
dp[i] = dp[i+1] + 1; //当不能匹配时
dp[i] = std::min(dp[i], dp[msg] + (msg-i) - len[j]); //当匹配时。
第i个字符到第msg个字符之间一共有msg-i个字符,减去字典中单词的长度,就是删除的字符数量。
#include <stdio.h>
#include <string.h>
#include <algorithm>
int n, m, dp[], len[];
char s[], dic[][];
int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
scanf("%s", s);
for(int i = ; i < n; i++)
{
scanf("%s", dic[i]);
len[i] = strlen(dic[i]);
}
dp[m] = ;
for(int i = m-; i >= ; i--)
{
dp[i] = dp[i+] + ;
for(int j = ; j < n; j++)
{
if(s[i] == dic[j][] && m-i >= len[j])
{
int msg = i+, cnt = ;
while(msg < m && dic[j][cnt])
{
if(s[msg++] == dic[j][cnt])
cnt++;
}
if(cnt == len[j])
dp[i] = std::min(dp[i], dp[msg] + (msg-i) - len[j]);
}
}
}
printf("%d\n", dp[]);
}
return ;
}
POJ 3267 The Cow Lexicon 简单DP的更多相关文章
- poj 3267 The Cow Lexicon(dp)
题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...
- POJ 3267 The Cow Lexicon
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- poj 3267 The Cow Lexicon (动态规划)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8167 Accepted: 3845 D ...
- 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 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
- POJ 3267为什么优先队列超时,DP就能过,难过
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11846 Accepted: 5693 Desc ...
- poj 1050 To the Max (简单dp)
题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...
- 【POJ 3176】Cow Bowling(DP)
题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...
- PKU 3267 The Cow Lexicon(动态规划)
题目大意:给定一个字符串和一本字典,问至少需要删除多少个字符才能匹配到字典中的单词序列.PS:是单词序列,而不是一个单词 思路: ...
随机推荐
- Android执行shell命令
一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/bin/cat", &q ...
- [Javascript] Adding Shapes to Maps with Leaflet and GeoJSON
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 动作-CCActionInterval之CCActionEase家族
补间动作也是一个包装器(之前叫补间动画,从字面上讲,叫动作更合适一些.).你也可以叫他缓释动作. 1.含义 补间动作改变的是,内部动作的执行速率(注意,并没有改变执行的最终效果,和执行的时间.)关于这 ...
- cocos2d-x 2.1.4学习笔记01:windows平台搭建cocos2d-x开发环境
cocos2d-x的大致开发流程是,首先使用win32版进行代码编写并完成游戏,然后将代码迁移到对应的开发环境上进行交叉编译完成游戏打包,如iphone上是mac+xcode,android是ecli ...
- -----------------------------MVC模式理解-----------------------------
1.用户发送请求到控制层: 2.控制层将接受到的请求转发到模型层: 3.模型层将请求结果转发到控制层: 4.控制层将模型层返回的结果进行视图渲染: 5.视图渲染后返回(jstl): 6.将视图展示给用 ...
- oracle学习----行级锁的理解
通过实验来理解行级锁的发生 1.创建需要的表 SQL> conn / as sysdba已连接.SQL> create table dept as select * from scott. ...
- php笔记04:get/post请求有两种主要方式
get/post的区别有哪些? 1. 安全性get请求的数据会显示在地址栏上,post请求的数据,放在http协议的消息体中 2. 从可以提交的数据大小来看: http协议本身并没有限制数据大 ...
- 安卓UI美化_drawable
UI美化 drawable资源 ------------------------------------------state drawable资源在不同状态下显示不同的图片在<selector ...
- YII中文件上传
文件上传 1.视图文件代码 <?php $form = $this->beginWidget("CActiveForm",array( "action&quo ...
- web页面的生命周期
1.先上几张原理图着重理解: 现在针对第四副图原理进行解析: 流程: 1.浏览器发送请求 2.服务器软件(IIS)接收,它最终的目的就是为了向客户输出它请求的动态页面生成的html代码. 3.服务器不 ...