继续大战dp。2018年11月30日修订,更新一下现在看到这个题目的理解(ps:就现在,poj又503了)。

题意分析

这条题目的大意是这样的,问一字符串内最少删去多少的字符使其由给定的若干字符串构成。非常好的一道字符串dp题。
具体的dp解法是什么呢?考虑一下我们删去的这个过程。比如说这个式子

throw
thraow

我们要不然删去a,要不然删去 “thraow”才能满足由第一个式子构成的这个条件(空串也算被第一个式子构造了)。但是,程序如何知道删去a是使这俩个单词匹配的最优决策?

我们这样考虑:每次从后往前考虑到第i个字符的时候,我有两个决策:一,这个字符不行,删掉(这是个始终合理的答案)。二,这个字符很行,从它($str_i$)到某个字符($str_j$)结束,是能够在删除若干字符的情况下匹配到某个目标串的(也就是说,某个目标串是$(i,j)$的子序列),那么从这个串到末尾的不能匹配的情况就变成(或者说“转移”)从那个目标串结束之后的字符开始的情况了(当然需要更新一下我删去的字符的数目)。

因此,有了上面这些思维过程,就可以想到设立$dp[i]$保存每次删去的最小值,并且从右往左去处理。这样,$dp[i]$的意思就是“从i->end删除最少的值”。
而转移方程自然很容易得出了:$$dp[i]={dp[i+1]+1,dp[cur]+L-cur-i}$$,第二个仅当目标字符串能够在[i,cur]中间删去若干字符后完成匹配可用。

代码

#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std; typedef unsigned long long ull;
int dp[305];
int main()
{
int w,l;
while(cin>>w>>l)
{
string wstr; cin>>wstr;
vector<string> lstr;
for(int i=1;i<=w;++i)
{
string tmp; cin>>tmp;
lstr.push_back(tmp);
}
dp[l]=0;
for(int i=l-1;i>=0;--i)
{
dp[i]=dp[i+1]+1;
//printf("dp[%d]=%d.\n",i,dp[i+1]+1);
for(int j=0;j!=w;++j)
{
int len=lstr[j].length();
if(len<=l-i && lstr[j][0]==wstr[i])
{
int curw=i,curl=0;
while(curw<l)
{
if(lstr[j][curl]==wstr[curw++])
curl++;
if(curl==len)
{
dp[i]=min(dp[i],dp[curw]+curw-i-len);
//printf("[Changed]dp[%d]=%d.\n",i,dp[i]);
break;
}
}
}
}
}
cout<<dp[0]<<endl;
}
return 0;
}

【个人训练】The Cow Lexicon(POJ-3267)的更多相关文章

  1. The Cow Lexicon POJ - 3267 dp

    题意  给出一个母串  和一个字典 问母串最少删去几个字母     删去后的母串是由字典里面的单词拼起来的 思路:dp[i]表示从i到母串结尾最少需要删除多少个字母  初始化dp[length]=0 ...

  2. POJ 3267:The Cow Lexicon(DP)

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

  3. POJ 3267 The Cow Lexicon

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

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

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

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

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

  6. POJ 3267-The Cow Lexicon(DP)

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

  7. POJ3267 The Cow Lexicon(DP+删词)

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

  8. The Cow Lexicon

    The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...

  9. HDOJ-三部曲-1015-The Cow Lexicon

    The Cow Lexicon Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

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

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

随机推荐

  1. POJ 1681 Painter's Problem 【高斯消元 二进制枚举】

    任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total ...

  2. 【洛谷P1073】[NOIP2009]最优贸易

    最优贸易 题目链接 看题解后感觉分层图好像非常NB巧妙 建三层n个点的图,每层图对应的边相连,权值为0 即从一个城市到另一个城市,不进行交易的收益为0 第一层的点连向第二层对应的点的边权为-w[i], ...

  3. 学大伟业 Day 3 培训总结

    今天讲的字符串: 不多说,直接看题 一.表达式求值 题目大意: 输入一行一个表达式,计算其答案 表达式包含非负整数.加减乘除.括号 两种做法 ·栈 ·表达式树 这里更推荐表达式树,因为栈是先压进去,逆 ...

  4. HDU 1220 Cube(数学,找规律)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1220 Cube Time Limit: 2000/1000 MS (Java/Others)    M ...

  5. 11java基础继承

    一.           继承相关   18.实现如下类之间的继承关系,并编写Music类来测试这些类. package com.hry.test; public class Instrument { ...

  6. php如何实现登陆后返回原页面

    访问网站页面时,有的页面需要授权才能访问,这时候就会要求用户登录,跳转到登录页面login.php,怎么实现登录后返回到刚才访问的页面项目需求 访问网站页面时,有的页面需要授权才能访问,这时候就会要求 ...

  7. 课时60.CSS的固定格式(掌握)

    CSS就是用来设置样式的,美化界面的 如何验证? 打开一个京东首页 删除掉css样式 发现页面变得非常难看 由此我们验证了一个说法,css就是用来美化界面的 1.格式: <style type= ...

  8. Python Json模块中dumps、loads、dump、load函数介绍哦

    来自: https://www.jb51.net/article/139498.htm 1.json.dumps()       json.dumps()用于将dict类型的数据转成str,因为如果直 ...

  9. boost::shared_ptr文档翻译

    shared_ptr: 共享所有权 原文链接 描述 模版类 shared_ptr 存储动态构造对象的指针,通常是由C++ new语句完成的.这个对象指针在最后一个持有指针所有权的shared_ptr被 ...

  10. CF1042C Array Product(贪心,模拟)

    题目描述 You are given an array aa consisting of nn integers. You can perform the following operations w ...