Typewriter

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 720    Accepted Submission(s): 225

Problem Description
Nowadays, our senior is typewriting an article for her graduation. But as you know our senior is foolish sometimes, so she needs your help. You can assume senior’s article is a string, and senior’s typewriter provides two operations to typewrite it:
1)  Assume you have typewritten a string s, then you can add a character after s, and the cost of adding each character is given to you.
2)  Assume you have typewritten a string s, you can select a substring of s and copy it, then you can paste it after s once. The cost to select a character is A,and the cost to copy and paste a string are all equal B.
Now, you should help senior to typewrite the article by using the minimum cost, can you help her?
 
Input
In the first line there is an integer T ( 1≤T≤100 ), indicating the number of test cases.
For each test case:
The first line includes a string s (1≤|s|≤10^5) composed by lowercase letters, indicating the article senior want to typewrite.
The second line includes 26 integers, indicating the cost to add character ‘a’, ‘b’, ‘c’…, and so on.
The third line includes two integers A and B.
All the integers in the input are in the range of [1,10^9]. It is guaranteed that the total length of s doesn't exceed 1.2×106.
 
Output
For each test case:
Please output “Case #k: answer”(without quotes) one line, where k means the case number count from 1, and the answer is the minimum cost to typewrite the article.
 
Sample Input
2
abc
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2
aaaa
10 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1
 
Sample Output
Case #1: 3
Case #2: 17
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  6730 6729 6728 6727 6726 
 

题意:

  给你一串字符以及单独打印每一个字母的代价,对于一个字母,你可以选择直接打印,也可以选择利用复制粘贴的方法来完成。对于复制粘贴,每次选择长度为i的字符串的代价是i*A,复制的代价是B,粘贴的代价也是B。即复制并粘贴一个长度为x的字符串的代价是x*A+2*B。现在问把这一整个字符串打印出来的代价是多少。
思路:

  考虑DP, dp[i]:表示前i个字符打印完成的最小代价,则我们可以得到dp[i]=min(dp[i-1]+val[s[i]] , dp[j]+ (i-j)*A+2*b);那么这样的DP的时间复杂度是O(n^2),对于1e6的数据来说是无法接受的。

我们定义j为最小的一个位置,可以使得1..j的子串包含j+1..i的子串。经过观察,我们可以发现,这个j一定是单调的。也就是说,随着i的变大,j一定只增不减,这个很好理解。如果对于上一个长度来说最小的位置是j,对于当前决策,增加了一个长度,j肯定是要么增加要么不变。所以我们就可以利用这个单调性,用单调队列来优化这个dp.

  如何保证在复杂度时间内判断某个串中是否包含另外一个串。也即,字符串s中,一个子串是否包含另外一个子串。如此,还是可能需要用到s的所有的子串,所以说还是用后缀自动机。每次,我们维护j,j的移动取决于前面部分的子串是否包含后面的子串。具体来说,对于当前的j,j+1~i-1在自动机上匹配到的位置x,如果对于新的i,自动机上面如果仍然有后继,那么j不动。如果没有,那么我就要考虑移动j了,移动的过程中,把新的s[j+1]添加到自动机中,继续匹配,如果添加到一定位置,发现x有s[i]这个后继了,那么停止;或者说,当j+1~i-1的长度缩短到x的parent指针所指节点的长度的时候,x则发生跳转,直接跳到其最长的后缀那里去。因为在移动的过程中,需要匹配的串也逐渐变短,短到一定程度就会变成原本串的一个最长后缀。就这样已知移动然后匹配,找到j移动到i或者发现点x有s[i]这个后继。

如此,对于当前i确定了j之后,我们就可以修改单调队列中的值,把所有小于j的决策都去掉,然后再选取目前最优的决策更新。最后把当前的状态结果也当作决策加入单调队列中。一边dp一边维护单调队列和j,同时还不断的把字符添加到SAM中。

参考代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pli pair<ll,int>
#define mkp make_pair
const int INF=0x3f3f3f3f;
const int maxn=1e5+;
int T;
char s[maxn];
int fa[maxn<<],l[maxn<<],nxt[maxn<<][];
int last,tot;
ll val[],dp[maxn],A,B;
pli q[maxn];
void Init()
{
last=tot=;
l[tot]=fa[tot]=;
memset(nxt[tot],,sizeof nxt[tot]);
} int NewNode()
{
++tot; fa[tot]=l[tot]=;
memset(nxt[tot],,sizeof nxt[tot]);
return tot;
} void Insert(int ch)
{
int np=NewNode(),p=last;
last=np; l[np]=l[p]+;
while(p&&!nxt[p][ch]) nxt[p][ch]=np,p=fa[p];
if(!p) fa[np]=;
else
{
int q=nxt[p][ch];
if(l[q]==l[p]+) fa[np]=q;
else
{
int nq=NewNode();
l[nq]=l[p]+;
memcpy(nxt[nq],nxt[q],sizeof nxt[q]);
fa[nq]=fa[q];
fa[np]=fa[q]=nq;
while(p&&nxt[p][ch]==q) nxt[p][ch]=nq,p=fa[p];
}
}
} int main()
{
scanf("%d",&T);
for(int cas=;cas<=T;++cas)
{
Init();
scanf("%s",s+);
int n=strlen(s+);
for(int i=;i<;++i) scanf("%lld",&val[i]);
scanf("%lld%lld",&A,&B);
int pos=;dp[]=;
int L=,R=,nt=,j=,len=,pa;
for(int i=;i<=n;++i)
{
int ch=s[i]-'a';
dp[i]=dp[i-]+val[ch];
pa=fa[pos]; nt=nxt[pos][ch];
while(!nt&&j+<i)
{
if(pos!=&&--len==l[pa]) pos=pa,pa=fa[pos];
++j; Insert(s[j]-'a');
nt=nxt[pos][ch];
}
if(!nt)
{
pos=;L=R=; ++j;
Insert(s[j]-'a');
}
else pos=nt,++len;
while(L<R && q[L].second<j) ++L;
if(L!=R) dp[i]=min(dp[i],q[L].first+1ll*i*A+2ll*B);
q[R++]=mkp(dp[i]-i*A,i);
while(L<R-&&q[R-].first<=q[R-].first) --R,q[R-]=q[R];
}
printf("Case #%d: %lld\n",cas,dp[n]);
} return ;
}

HDU5470 Typewriter (SAM+单调队列优化DP)的更多相关文章

  1. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  2. bzoj1855: [Scoi2010]股票交易--单调队列优化DP

    单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...

  3. hdu3401:单调队列优化dp

    第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...

  4. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  5. BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP

    BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...

  6. 【单调队列优化dp】 分组

    [单调队列优化dp] 分组 >>>>题目 [题目] 给定一行n个非负整数,现在你可以选择其中若干个数,但不能有连续k个数被选择.你的任务是使得选出的数字的和最大 [输入格式] ...

  7. [小明打联盟][斜率/单调队列 优化dp][背包]

    链接:https://ac.nowcoder.com/acm/problem/14553来源:牛客网 题目描述 小明很喜欢打游戏,现在已知一个新英雄即将推出,他同样拥有四个技能,其中三个小技能的释放时 ...

  8. 单调队列以及单调队列优化DP

    单调队列定义: 其实单调队列就是一种队列内的元素有单调性的队列,因为其单调性所以经常会被用来维护区间最值或者降低DP的维数已达到降维来减少空间及时间的目的. 单调队列的一般应用: 1.维护区间最值 2 ...

  9. BZOJ1791[Ioi2008]Island 岛屿 ——基环森林直径和+单调队列优化DP+树形DP

    题目描述 你将要游览一个有N个岛屿的公园.从每一个岛i出发,只建造一座桥.桥的长度以Li表示.公园内总共有N座桥.尽管每座桥由一个岛连到另一个岛,但每座桥均可以双向行走.同时,每一对这样的岛屿,都有一 ...

随机推荐

  1. 小程序的基本概念-生命周期(组件 wxml)

    一.组件生命周期:一个组件从创建开始到使用中最后被销毁的过程 ---onLoad事件:组件(创建成功并且加载完成)触发一次 (1)当此事件触发发送请求获取数据 (2)获取其他组件传递数据(option ...

  2. VS链接文件设置

    右键点击文件夹,添加现有项,选中文件,添加为链接 ,点击确定,那么在修改源文件后这个目录的文件也会同步修改.如果更改源文件目录,就需要重新指定一次链接.

  3. Python基础教程(第3版)学习笔记

    第1章.基础 1.几个小知识点 多用 help() 帮助文档 除法运算 / 除法运算,得到结果为浮点数: // 整除运算,得到整数值(向下取整): % 取余操作 (结果符号与除数符号相同),本质上: ...

  4. 阿里云ECS服务器部署HADOOP集群(二):HBase完全分布式集群搭建(使用外置ZooKeeper)

    本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建,多添加了一个 datanode 节点 . 1 节点环境介绍: 1.1 环境介绍: 服务器:三台阿里 ...

  5. 在lldb调试中调用c++函数 - 如何使用QuartzCore里面的日志消息

    承接上一篇,上一篇讲到可以在lldb调试中调用QuartzCore.framework里的CA::Render::Object::show方法来是观察CA::Render模块内的类的信息,但是在lld ...

  6. opencv 3 core组件进阶(2 ROI区域图像叠加&图像混合;分离颜色通道、多通道图像混合;图像对比度,亮度值调整)

    ROI区域图像叠加&图像混合 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp&g ...

  7. convert svn repo to git

    https://john.albin.net/git/convert-subversion-to-git 1. 抓取Log 在linux 上做的,其余是在win上做的. 2. svn co svn:/ ...

  8. 【2018寒假集训 Day1】【位运算】翻转游戏

    翻转游戏(flip) [问题描述] 翻转游戏是在一个 4 格×4 格的长方形上进行的,在长方形的 16 个格上每 个格子都放着一个双面的物件.每个物件的两个面,一面是白色,另一面是黑色, 每个物件要么 ...

  9. MySQL 、PDO对象

    目录 1, singleton 2, pdo与db 3, singleton获取pdo 4, pdo实现db增删改查 5, pdo异常处理exception 6, pdo预处理prepare 7, p ...

  10. 为什么坚果云Markdown值得使用?

    值得使用说明是被认为有价值的,值得有两种意思的解释:1.有价值,有意义:2.价钱合适,合算:那么坚果云Markdown是否是人们值得使用的呢?下面就来看看坚果云Markdown到底是什么?为什么值得使 ...