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(动态规划)的更多相关文章

  1. poj 3267 The Cow Lexicon (动态规划)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8167   Accepted: 3845 D ...

  2. POJ 3267 The Cow Lexicon

    又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...

  3. poj 3267 The Cow Lexicon(dp)

    题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...

  4. POJ 3267 The Cow Lexicon 简单DP

    题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...

  5. PKU 3267 The Cow Lexicon(动态规划)

    题目大意:给定一个字符串和一本字典,问至少需要删除多少个字符才能匹配到字典中的单词序列.PS:是单词序列,而不是一个单词 思路:                                     ...

  6. POJ3267——The Cow Lexicon(动态规划)

    The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...

  7. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

  8. POJ 3267:The Cow Lexicon 字符串匹配dp

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 4228 D ...

  9. POJ 3267-The Cow Lexicon(DP)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8252   Accepted: 3888 D ...

随机推荐

  1. Selenium的自我总结2_元素基本操作

    对于Selenium的基本元素的操作,就自己的了解做了一个基本的介绍,这篇直接上代码,针对一个页面如何操作写了些基本的操作脚本,希望对初学者有一定的帮助,也希望通过这些总结让自己有一些清晰的认识和了解 ...

  2. 键盘事件(keyup、keydown、keypress)

    1.onkeyup 和onkeydown时,keyCode是不区分大小写的,会将小写字母自动转化为大写字母. 2 onkeypress时,区分大小写. 3兼容event.keyCode||event. ...

  3. eclipse里面找不到databaseexplorer

    在window==>show view==>Other==>Data Management==>Database explorer配置:在右下方点击Database Sourc ...

  4. 软件工程_3rd weeks

    本周上课的第一件事就是四人组队做工程,因为之前没有太多的准备,所以过程有些仓促,只是最后的结果是好的.有了自己的队伍和课题.感觉就应该这样,平时的我们比较随意,没有一丝的紧迫感,这样会督促着我们完成任 ...

  5. python数据相关性分析 (计算相关系数)

    #-*- coding: utf-8 -*- #餐饮销量数据相关性分析 计算相关系数 from __future__ import print_function import pandas as pd ...

  6. js函數

    函數是什麼?函數就是被事件驅動或者調用執行的可重複的代碼塊. 函數聲明: 使用關鍵詞function,關鍵詞function大小敏感. function a{代碼塊} 局部變量: 在函數內部聲明的變量 ...

  7. Spring MVC @ModelAttribute注解

    在一个Controller内,被@ModelAttribute标注的方法会在此controller的每个handler方法执行前被执行. 被@ModelAttribute标注的方法的参数绑定规则和普通 ...

  8. Bootstrap媒体对象

    前面的话 在Web页面或者说移动页面制作中,常常看到图文混排效果,图片居左(或居右),内容居右(或居左)排列.常常把这样的效果称为媒体对象.可以说它是一种抽象的样式,可以用来构建不同类型的组件.本文将 ...

  9. matlab中输入x. 与x的区别

    参考:http://www.madio.net/thread-27742-1-1.html 点乘的话,是对应元素相乘. 没点的话,按照矩阵相乘法则进行,这就要求前一个矩阵的列数等于第二个矩阵的行数. ...

  10. [资源]--IOS捷径大全,众多实用小功能

    一.实用工具 1.支付助手3.0(新) (扫一扫.微信扫码.微信收款.支付宝扫码.Apple Pay.AA付款.查快递.蚂蚁森林.蚂蚁庄园.彩票.股票.运动.淘票票.乘车码.生活缴费.火车票等等): ...