POJ - 3267 The Cow Lexicon(动态规划)
https://vjudge.net/problem/POJ-3267
题意
给一个长度为L的字符串,以及有W个单词的词典。问最少需要从主串中删除几个字母,使其可以由词典的单词组成。
分析
状态设置很关键,设dp[i]表示以i为起始的后缀需要删去字母的最小数目。那么根据状态,必须从后往前遍历,现在考虑往前加入一个新字母,会发生什么呢?第一,考虑最坏情况,就是加进来的字母没有用处,即转移成dp[i+1]+1;第二,就是加入这个字母后,以这个字母开始的串可以删去一些字母从而符合要求,那么此时就要计算了匹配到的位置,匹配过程:逐字匹配,字符相同则两个指针同时向后移动一次,否则pj固定,pi移动。当因为pi>L跳出匹配时,说明匹配失败,dp[i]状态不变;当pj==单词长度时,单词匹配成功,进行dp[i]的状态优化。此时dp[i]=min{dp[i],dp[pi]+pi-i-len}。pi-i是匹配区间的长度,于是pi-i-len则为需要删除的字母数目。
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(),c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
//const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T; void testcase(){
printf("Case %d:",++T);
} const int MAXN = 5e5+ ;
const int MAXM = ;
const double eps = 1e-;
const double PI = acos(-1.0); char msg[];
char word[][];
int dp[];
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int W,L;
scanf("%d%d",&W,&L);
scanf("%s",msg);
for(int i=;i<W;i++) scanf("%s",word[i]);
dp[L]=;
for(int i=L-;i>=;i--){
dp[i]=dp[i+]+;
for(int j=;j<W;j++){
int len=strlen(word[j]);
int pi=i,pj=;
while(pi<L){
if(msg[pi++]==word[j][pj]){
pj++;
}
if(pj==len){
dp[i]=min(dp[i],dp[pi]+pi-i-len);
break;
}
}
}
}
cout<<dp[]<<endl;
return ;
}
POJ - 3267 The Cow Lexicon(动态规划)的更多相关文章
- 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
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- 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 ...
随机推荐
- 原生NodeJs制作一个简易聊天室
准备工作 安装NodeJs环境 安装编译器Sublime 如果网速不理想,可以百度一下如何加快npm的速度~ 使用node搭建一个简单的网站后台 做完准备工作之后,新建文件夹chatroom,在cha ...
- Lodop强制分页LODOP.NewPage()和LODOP.NewPageA()
使用Lodop打印控件打印时,有自动分页,有手动强制分页,也可以两者结合使用,在使用两者结合的时候注意LODOP.NewPage()和LODOP.NewPageA()的区别,如果前面打印项自动分页不止 ...
- XQN number format
Q Numbers FormatAn XQN format number is an 1+X+N bit twos complement binary number; a sign bitfollow ...
- day27 多继承 super 详细用法
# 没有使用super的时候的多继承,如果父类的名字变了.或者有什么更改,需要全部都一起改 class FooParent: def bar(self, message): print(message ...
- MT【16】证明无理数(2)
证明:$sin10^0$为无理数. 分析:此处用$sin$的三倍角公式,结合多项式有有理根必须满足的系数之间的关系可以证明. 评:证明$sin9^0$为无理数就不那么简单.思路:先利用$sin54^0 ...
- 【POI每日题解 #9】SKA-Piggy Banks
题目链接 题意: 有一棵环套树 求最少从多少个节点出发能沿边走过整棵树 环套树 并查集求联通块 有几块就砸几个 太简单不发代码了 不过某大佬的环套树找环dfs让我研究了好久… 贴一下以Orz #inc ...
- 数据挖掘(二)用python实现数据探索:汇总统计和可视化
今天我们来讲一讲有关数据探索的问题.其实这个概念还蛮容易理解的,就是我们刚拿到数据之后对数据进行的一个探索的过程,旨在了解数据的属性与分布,发现数据一些明显的规律,这样的话一方面有助于我们进行数据预处 ...
- AutoCompleteTextView 简单用法 实现自定义list adapter
网上有不少教程,那个提示框字符集都是事先写好的,例如用一个String[] 数组去包含了这些数据,但是,我们也可以吧用户输入的作为历史记录保存 下面先上我写的代码:import andro ...
- SpaceVim的基本安装和常见问题
SpaceVim官网:https://spacevim.org/ SpaceVim中文官网:http://spacevim.org/cn/ SpaceVim的Github页面:https://gith ...
- 最最最最最基础的SQL Server
--创建数据库(命名不允许为汉字开头.不允许为数字开头.不允许为符号开头) create database Class --执行完成进行下一步点加go go --定位数据库 use Class --创 ...