水是水,但是写出了不少问题,因此写一发博客。

https://www.luogu.org/problemnew/show/P2178

https://www.lydsy.com/JudgeOnline/problem.php?id=4199

http://uoj.ac/problem/131

首先我们知道,反串SAM就是原串的后缀树,所以两个后缀的LCP就是反串SAM parent树上的LCA。

可是这个LCP的长度绝对不是LCA的深度,而应该是LCA的len. 所以我们对于每个节点统计它作为LCA的祖先,也就是从它的子树里选出两个的方案数,然后给\([len_{fa_i}+1,len_i]\)区间加上这个数。

另外一点是我们只需要记录最大次大和最小次小,其余的都是多余的,比如最小正数和最大负数啥的,都没有必要记录。

合并最大和次大的过程要特别注意,容易错。

最后就是后缀自动机上所有点的\(r-l+1\)之和是\(O(n^2)\)级别的,会T,因此需要差分。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define llong long long
using namespace std; const int N = 3e5;
const int S = 26;
const llong INF = (1ll<<61);
int n;
struct SuffixAutomaton
{
int oid[(N<<1)+3],buc[(N<<1)+3];
int fa[(N<<1)+3],son[(N<<1)+3][S+3],len[(N<<1)+3],sz[(N<<1)+3];
int dep[(N<<1)+3];
llong mx1[(N<<1)+3],mn1[(N<<1)+3],mx2[(N<<1)+3],mn2[(N<<1)+3];
llong ans1[N+3],ans2[N+3];
llong a[(N<<1)+3];
int siz,lstpos,rtn;
void init() {siz = lstpos = rtn = 1;}
void insertchar(char ch,llong x)
{
int p = lstpos,np; siz++; np = lstpos = siz; sz[np] = 1; len[np] = len[p]+1; a[np] = x;
for(; p && son[p][ch]==0; p=fa[p]) son[p][ch] = np;
if(p==0) {fa[np] = 1;}
else
{
int q = son[p][ch];
if(len[q]==len[p]+1) {fa[np] = q;}
else
{
siz++; int nq = siz; len[nq] = len[p]+1; a[nq] = -(1<<30);
memcpy(son[nq],son[q],sizeof(son[q]));
fa[nq] = fa[q]; fa[q] = fa[np] = nq;
for(; p && son[p][ch]==q; p=fa[p]) son[p][ch] = nq;
}
}
}
void SAMsort()
{
for(int i=1; i<=siz; i++) buc[i] = 0;
for(int i=1; i<=siz; i++) buc[len[i]]++;
for(int i=1; i<=siz; i++) buc[i] += buc[i-1];
for(int i=siz; i>=1; i--) oid[buc[len[i]]--] = i;
dep[rtn] = 0;
for(int i=1; i<=siz; i++) mx1[i] = mx2[i] = -INF,mn1[i] = mn2[i] = INF;
for(int i=0; i<=n; i++) ans2[i] = -INF;
for(int i=2; i<=siz; i++)
{
int u = oid[i];
dep[u] = dep[fa[u]]+1;
if(sz[u]==0) continue;
mx1[u] = mn1[u] = a[u];
}
for(int i=siz; i>=2; i--)
{
int u = oid[i];
sz[fa[u]] += sz[u];
if(mx1[u]>=mx1[fa[u]])
{
mx2[fa[u]] = max(mx1[fa[u]],mx2[u]);
mx1[fa[u]] = mx1[u];
}
else
{
if(mx1[u]>mx2[fa[u]]) {mx2[fa[u]] = mx1[u];}
}
if(mn1[u]<=mn1[fa[u]])
{
mn2[fa[u]] = min(mn1[fa[u]],mn2[u]);
mn1[fa[u]] = mn1[u];
}
else
{
if(mn1[u]<mn2[fa[u]]) {mn2[fa[u]] = mn1[u];}
}
}
len[0] = -1;
for(int i=1; i<=siz; i++)
{
int u = oid[i];
llong num = (llong)sz[u]*((llong)sz[u]-1ll)/2ll;
llong mxp = max(mx2[u]==-INF ? -INF : mx1[u]*mx2[u],mn2[u]==INF ? -INF : mn1[u]*mn2[u]);
ans1[len[fa[u]]+1] += num; ans1[len[u]+1] -= num; ans2[len[u]] = max(ans2[len[u]],mxp);
}
for(int i=1; i<=n; i++) ans1[i] += ans1[i-1];
for(int i=n-1; i>=0; i--) ans2[i] = max(ans2[i],ans2[i+1]);
for(int i=0; i<n; i++) {printf("%lld %lld\n",ans1[i],ans1[i]==0ll ? 0ll : ans2[i]);}
}
} sam;
char str[N+3];
llong a[N+3]; int main()
{
sam.init();
scanf("%d",&n);
scanf("%s",str+1);
for(int i=1; i<n+1-i; i++) swap(str[i],str[n+1-i]);
for(int i=n; i>=1; i--) scanf("%lld",&a[i]);
for(int i=1; i<=n; i++) sam.insertchar(str[i]-96,a[i]);
sam.SAMsort();
return 0;
}

UOJ #131 BZOJ 4199 luogu P2178【NOI2015】品酒大会 (后缀自动机、树形DP)的更多相关文章

  1. BZOJ.4199.[NOI2015]品酒大会(后缀自动机 树形DP)

    BZOJ 洛谷 后缀数组做法. 洛谷上SAM比SA慢...BZOJ SAM却能快近一倍... 只考虑求极长相同子串,即所有后缀之间的LCP. 而后缀的LCP在后缀树的LCA处.同差异这道题,在每个点处 ...

  2. 洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)

    题意 题目链接 Sol 说一个后缀自动机+线段树的无脑做法 首先建出SAM,然后对parent树进行dp,维护最大次大值,最小次小值 显然一个串能更新答案的区间是\([len_{fa_{x}} + 1 ...

  3. BZOJ 2806 Luogu P4022 [CTSC2012]Cheat (广义后缀自动机、DP、二分、单调队列)

    题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=2806 (luogu) https://www.luogu.org/pro ...

  4. 【bzoj4199】[Noi2015]品酒大会 后缀自动机求后缀树+树形dp

    题目描述(转自百度文库) 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加. 在大会的晚餐上,调酒 ...

  5. 【BZOJ 4199】[Noi2015]品酒大会 后缀自动机+DP

    题意 两个长度为$r$的子串相等称为$r$相似,两个$r$相似的权值等于子串开头位置权值乘积,给定字符串和每个位置权值,求$r$相似子串数量和最大权值乘积 对反串建立后缀自动机得到后缀树,后缀树上两个 ...

  6. BZOJ 4199: [Noi2015]品酒大会 后缀自动机_逆序更新

    一道裸题,可以考虑自底向上去更新方案数与最大值. 没啥难的 细节........ Code: #include <cstdio> #include <algorithm> #i ...

  7. luoguP2178 [NOI2015]品酒大会(后缀自动机)

    题意 承接上篇题解 考虑两个后缀的\(lcp\)是什么,是将串反着插入后缀自动机后两个前缀(终止节点)的\(lca\)!!!于是可以在parent tree上DP了. 比后缀数组又简单又好写跑的还快. ...

  8. [LOJ 2133][UOJ 131][BZOJ 4199][NOI 2015]品酒大会

    [LOJ 2133][UOJ 131][BZOJ 4199][NOI 2015]品酒大会 题意 给定一个长度为 \(n\) 的字符串 \(s\), 对于所有 \(r\in[1,n]\) 求出 \(s\ ...

  9. 洛谷P2178 [NOI2015]品酒大会 后缀数组+单调栈

    P2178 [NOI2015]品酒大会 题目链接 https://www.luogu.org/problemnew/show/P2178 题目描述 一年一度的"幻影阁夏日品酒大会" ...

随机推荐

  1. C++对象内存布局 (一)

    一.前言 在看了皓哥的C++对象的布局之后:http://blog.csdn.net/haoel/article/details/3081328.感觉自己还是总是不能记得很清楚,故在此总结一下C++对 ...

  2. tflearn mnist 使用MLP 全连接网络一般都会加dropout哇

    # -*- coding: utf-8 -*- """ Deep Neural Network for MNIST dataset classification task ...

  3. python多线程,限制线程数

    #encoding:utf8 import threading import time data = 0 def func(sleeptime): global data print threadin ...

  4. (Go)07.strings与strconv的示例

    package main import ( "strconv" "fmt" "strings" ) func main() { str := ...

  5. hdu 3037Saving Beans(卢卡斯定理)

    Saving Beans Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  6. Django day06 模版层(一) 变量和深度查询

    一.模版语法之变量:  1  - {{ 变量 }} ******重要*******{#这个相当于print了该变量#} def index(request): name = 'prince' #字符串 ...

  7. git clone 出现错误

    看了好多资料终于搞定了git 中clone命令报错这个问题,废话不多说直接上步骤希望对大家有帮助. 1   删除.ssh文件夹(直接搜索该文件夹)下的known_hosts(手动删除即可,不需要git ...

  8. C - New Year Candles

    Problem description Vasily the Programmer loves romance, so this year he decided to illuminate his r ...

  9. C - Anton and Danik

    Problem description Anton likes to play chess, and so does his friend Danik. Once they have played n ...

  10. 【Codeforces】Codeforces Round #373 (Div. 2) -C

    C. Efim and Strange Grade Efim just received his grade for the last test. He studies in a special sc ...