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 ...
随机推荐
- 利用ss-redir加速服务器上国外服务的访问
https://blog.microdog.me/2016/06/28/Speed-Up-Network-Accessing-To-Overseas-Services-On-Your-Server/
- 服务器端发送邮件签名采用Data URI scheme包含图片
要在服务器端基于HTML,拼接邮件内容,原来用户使用outlook采用了邮件签名,签名里含有公司Logo的图片,Outlook的msg文件里是专有的cid:xxxx,这里借用Data URI sche ...
- [转帖]从1G到5G
一文看懂无线通信:从1G到5G 投递人 itwriter 发布于 2019-02-03 15:25 评论(2) 有280人阅读 [收藏] « » 文/老和山下的小学僧 最近太邪乎,先引用一个马克思政治 ...
- SQLSERVER 创建对Oracle数据库的DBlink以及查询使用
1. 与针对oracle数据库一样, 在sqlserver中创建对oracle数据库的dblink 安全性上面也进行定义(貌似不需要跟访问字符串只需要填一个即可) 发现有的版本改注册表不管用 还得修改 ...
- Android控件第3类——AdapterView
AdapterView这一类控件的最大特点,在绝大多数的情况下,它们的数据都由Adapter的子类提供(有时可以在控件的entries属性上直接设置显示的数据). 调用AdapterView的setA ...
- SourceTree 如何下载git 管理的代码-如何创建分支,删除分支,提交代码,回退代码
把用户给的链接拿过来,然后输入浏览器,然后在左侧会有Actions 中有个Clone;点击Clone之后,有个 Clone in Source Tree 点击,打开你的本地Source Tree,然后 ...
- 从网上整理的一些delphi字符串加密解密方法
function Encode(Str: string): string; var //加密 TmpChr: AnsiChar; i, Len: integer; begin Result := St ...
- BZOJ1087[SCOI2005]互不侵犯——状压DP
题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 输入 只有一行,包含两个数N,K ( ...
- ajax 调用 java webapi 多个参数(二)
第一种方法:http://blog.csdn.net/hanjun0612/article/details/74436273 附上另一种解决方法. 这个方法主要针对 嵌套模型(模型中含有模型)的ap ...
- 自学Zabbix6.1 Event acknowledgment 事件确认
自学Zabbix6.1 Event acknowledgment 事件确认 1 概述以往服务器出现报警,运维人员处理完事之后,报警自动取消,但是下一次出现同样一个错误,但是换了一个运维人员,他可能需要 ...