继续大战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. CodeForces-822D 【最小素因子应用】

    任意门:https://vjudge.net/problem/CodeForces-822D D. My pretty girl Noora time limit per test 1.5 secon ...

  2. Positioning-Based Photo Retrieval

    MMAI 2015 FINAL PROJECT   To Know Where We Are: Positioning-based Photo Retrieval   2015/12/16 Updat ...

  3. 【洛谷P1005】矩阵取数游戏

    矩阵取数游戏 题目链接 每行分别跑一趟区间DP即可 这道题区间DP是非常裸的,按套路来即可 但是很毒瘤的是需要高精度, “我王境泽就是爆零,从这跳下去,也不会用__int128的!” #include ...

  4. linux系统状态查看/管理相关命令

    系统状态查看命令: w 查看用户 top 系统进程监控 uptime 查看某台服务器运行了多久 htop 更加先进的交互式监控工具(需要安装) iotop 监控并实时显示磁盘IO输入和输出和程序进程( ...

  5. SAC E#1 - 一道难题 Tree(树形DP)

    题目背景 冴月麟和魏潇承是好朋友. 题目描述 冴月麟为了守护幻想乡,而制造了幻想乡的倒影,将真实的幻想乡封印了.任何人都无法进入真实的幻想乡了,但是她给前来救她的魏潇承留了一个线索. 她设置了一棵树( ...

  6. 【TOJ 3692】紧急援救

    #include<iostream> #include<algorithm> #include<queue> using namespace std; #defin ...

  7. 使用 form 和 iframe 实现图片上传回显

    主要利用 form 的 target 属性,在提交表单之后 response 返回到 iframe 中 form 的 action 可以自己写,也可以直接利用 富文本编辑器的接口实现上传 <fo ...

  8. BZOJ3098: Hash Killer II(构造)

    Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 2162  Solved: 1140[Submit][Status][ ...

  9. spring boot 搭建基本套路《1》

    1. Spring复习 Spring主要是创建对象和管理对象的框架. Spring通过DI实现了IoC. Spring能很大程度的实现解耦. 需要掌握SET方式注入属性的值. 需要理解自动装配. 需要 ...

  10. centos7下双网卡绑定

    一.进入网卡配置目录 cd /etc/sysconfig/network-scripts 二.备份原有网卡 mv ifcfg-em* /tmp/ 三.配置双网卡 nmcli con add type ...