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 ...
随机推荐
- Maven-Build Lifecycle(构建生命周期)
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html https://www.w3cschoo ...
- MyBatis 集合操作语法范例:配合SQL的in关键字
Java语法: private String[] tagIds; MyBatis语法 <delete id="deleteByIds" parameterType=" ...
- C语言复制文件的两种简单的方法【从根本解决问题】
网上的方法大致有这样几种: 1.使用操作系统提供的复制文件的API 2.使用C语言本身提供的复制文件的函数 3.直接读写文件,从文件角度来操作,从而直接将一个文件复制 这里我们使用的就是这第三种. 复 ...
- 被辞退时N+1的说法
“N+”,指在这家公司工作了N年,赔偿N个月的上年平均工资,再加上1个月“代通知金”. N的上限时12: 上年平均工资的上限是工作所在城市平均工资的三倍. StudyFrom知乎 所以很明显能够算出来 ...
- 日常工作中 Oracle12c参数的处理:
1. Oracle12c 修改过的参数 alter system set result_cache_mode=manual; alter system ; alter system set " ...
- 类的三大方法 与__init___
静态方法 存储在类中 实例方法 与类方法都能调用 不需要 传递self 相当于在类里面写了一个函数 类方法 存储在类中 只能是类调用 普通方法 存储在类中 由实例对象使用 在实例化类的时候 就相 ...
- 51nod-1459-迷宫游戏
题意:中文题目.. 解题思路:我的做法就是单源最短路中加个记录分数的数组,如果dis[i]到dis[x]的距离可以被优化,那就连记录分数的数组一起优化,如果第二条路和第一条路的距离相等,那就取最大的分 ...
- CentOS 显示历史执行过的命令以及用户历史命令缓存文件
1.history命令用于显示历史执行过的命令 执行 history命令能显示出当前用户在本地计算机中执行过的最近 1000 条命令记录. 如果觉得 1000 不够用,还可以自定义/etc/profi ...
- 自学Linux Shell9.1-安装软件程序
点击返回 自学Linux命令行与Shell脚本之路 9.1-linux安装软件程序 PMS利用一个数据库来记录各种相关内容: Linux系统安装了什么软件包 每个包安装什么文件 每个已安装软件包的版本 ...
- Android 安装 卸载 更新 程序
安装程序的方法: .通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据. 1. String fileName =Environment.getExternalStorage ...