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:是单词序列,而不是一个单词 思路: ...
随机推荐
- [TypeScript] 0.First Example
Create a greeter.ts file: class Student { fullname : string; constructor(public firstname, public mi ...
- crash recovery
2016-07-02 17:56:07 5772 [Note] InnoDB: Database was not shutdown normally!2016-07-02 17:56:07 5772 ...
- 【转】如何高效利用GitHub——2013-08-28 22
http://www.yangzhiping.com/tech/github.html 正是Github,让社会化编程成为现实.本文尝试谈谈GitHub的文化.技巧与影响. Q1:GitHub是什么 ...
- PPPoE Server Under Ubuntu/Debian
http://imranasghar.blogspot.com/2009/05/pppoe-server-under-ubuntudebian.html ----------------------- ...
- Java基础知识强化之IO流笔记41:字符流缓冲流之复制文本文件案例02(使用 [ newLine() / readLine() ] )(重要)
1. 使用字符流缓冲流的特殊功能 [ newLine() / readLine() ] 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 数据源: a.txt -- 读取数据 ...
- 关于ActiveMQ的问题分析
目前常用的消息队列组建无非就是MSMQ和ActiveMQ,至于他们的异同,这里不想做过多的比较.简单来说,MSMQ内置于微软操作系统之中,在部署上包含一个隐性条件:Server需要是微软操作系统.(对 ...
- Linux 普通用户su命令切换控制
1.编辑配置文件/etc/pam.d/su .将下面配置文件"#“去掉: # auth required pam_wheel.so use_uid 改成 a ...
- php 5.3+ 连接mssql
php5.3+里已经没有mssql的dll扩展了,需要使用SQL Server Driver for PHP 这里有两个版本有两个版本支持不同的php版本. 1.SQL Server Driver f ...
- 玩转html5<canvas>画图
导航 前言 基本知识 绘制矩形 清除矩形区域 圆弧 路径 绘制线段 绘制贝塞尔曲线 线性渐变 径向渐变(发散) 图形变形(平移.旋转.缩放) 矩阵变换(图形变形的机制) 图形组合 给图形绘制阴影 绘制 ...
- HTML DOM对象
HTML DOM对象 Document对象每个载入浏览器的HTML文档都会成为Document对象Document对象让我们可以从javascript中操作文档中的所有元素Document对象是win ...