BZOJ3238 [Ahoi2013]差异
首先把后缀数组和height数组都搞出来。。。
然后用两个单调栈维护$[l, r]$表示对于一个点$x$,满足$height[x] \le height[l..x] \ \&\&\ height[x] < height[x..r]$的最小的$l$和最大的$r$
这样子就可以保证不会重复计算了
/**************************************************************
Problem: 3238
User: rausen
Language: C++
Result: Accepted
Time:4496 ms
Memory:20336 kb
****************************************************************/ #include <cstdio>
#include <cstring> using namespace std;
typedef long long ll;
const int N = 5e5 + ; int a[N], len;
int sa[N], rank[N], height[N]; inline void Sort(int *a, int *b, int *c, int n, int m) {
static int i, sum[N];
for (i = ; i <= m; ++i) sum[i] = ;
for (i = ; i < n; ++i) ++sum[c[a[i]]];
for (i = ; i <= m; ++i) sum[i] += sum[i - ];
for (i = n - ; ~i; --i)
b[--sum[c[a[i]]]] = a[i];
} void make_sa(int *s) {
int i, j;
static int x[N], y[N];
for (i = ; i < len; ++i) x[i] = s[i], rank[i] = i;
Sort(rank, sa, x, len, );
rank[sa[]] = ;
for (i = ; i < len; ++i)
rank[sa[i]] = rank[sa[i - ]] + (x[sa[i]] != x[sa[i - ]]);
for (i = ; i <= len; i <<= ) {
for (j = ; j < len; ++j)
x[j] = rank[j], y[j] = j + i < len ? rank[j + i] : , sa[j] = j;
Sort(sa, rank, y, len, len), Sort(rank, sa, x, len, len);
rank[sa[]] = ;
for (j = ; j < len; ++j)
rank[sa[j]] = rank[sa[j - ]] + (x[sa[j]] != x[sa[j - ]] || y[sa[j]] != y[sa[j - ]]);
if (rank[sa[len - ]] == len) return;
}
} void make_height() {
int i, j;
for (i = j = ; i < len; ++i) {
if (j) --j;
if (rank[i] != )
while (a[i + j] == a[sa[rank[i] - ] + j]) ++j;
height[rank[i]] = j;
}
} ll work() {
int i;
ll res;
static int s[N], top, l[N], r[N];
for (res = , i = ; i <= len; ++i) res += 1ll * i * (len - );
for (s[top = ] = , i = ; i <= len; ++i) {
while (height[i] <= height[s[top]] && top) --top;
l[i] = s[top] + ;
s[++top] = i;
}
for (s[top = ] = len + , i = len; i; --i) {
while (height[i] < height[s[top]] && top) --top;
r[i] = s[top] - ;
s[++top] = i;
}
for (i = ; i <= len; ++i)
res -= 2ll * (i - l[i] + ) * (r[i] - i + ) * height[i];
return res;
} int main() {
int i;
char ch;
for (len = ; ;) {
ch = getchar();
if ('a' <= ch && ch <= 'z') a[len++] = ch - 'a' + ;
else break;
}
make_sa(a);
make_height();
printf("%lld\n", work());
return ;
}
BZOJ3238 [Ahoi2013]差异的更多相关文章
- BZOJ3238 [Ahoi2013]差异 【SAM or SA】
BZOJ3238 [Ahoi2013]差异 给定一个串,问其任意两个后缀的最长公共前缀长度的和 1.又是后缀,又是\(lcp\),很显然直接拿\(SA\)的\(height\)数组搞就好了,配合一下单 ...
- bzoj3238 [Ahoi2013]差异 后缀数组+单调栈
[bzoj3238][Ahoi2013]差异 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Ou ...
- [bzoj3238][Ahoi2013]差异_后缀数组_单调栈
差异 bzoj-3238 Ahoi-2013 题目大意:求任意两个后缀之间的$LCP$的和. 注释:$1\le length \le 5\cdot 10^5$. 想法: 两个后缀之间的$LCP$和显然 ...
- [BZOJ3238][AHOI2013]差异(后缀数组)
求和式的前两项可以直接算,问题是对于每对i,j计算LCP. 一个比较显然的性质是,LCP(i,j)是h[rk[i]+1~rk[j]]中的最小值. 从h的每个元素角度考虑,就是对每个h计算有多少对i,j ...
- [BZOJ3238][Ahoi2013]差异解题报告|后缀数组
Description 先分析一下题目,我们显然可以直接算出sigma(len[Ti]+len[Tj])的值=(n-1)*n*(n+1)/2 接着就要去算这个字符串中所有后缀的两两最长公共前缀总和 首 ...
- BZOJ3238 [Ahoi2013]差异 【后缀数组 + 单调栈】
题目链接 BZOJ3238 题解 简单题 经典后缀数组 + 单调栈套路,求所有后缀\(lcp\) #include<iostream> #include<cstdio> #in ...
- BZOJ3238: [Ahoi2013]差异 (后缀自动机)
Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...
- bzoj千题计划314:bzoj3238: [Ahoi2013]差异(后缀数组+st表+单调栈)
https://www.lydsy.com/JudgeOnline/problem.php?id=3238 跟 bzoj3879 差不多 #include<cstdio> #include ...
- 2018.12.21 bzoj3238: [Ahoi2013]差异(后缀自动机)
传送门 后缀自动机好题. 题意: 做法:samsamsam 废话 考虑翻转字串,这样后缀的最长公共前缀等于前缀的最长公共后缀. 然后想到parentparentparent树上面两个串的最长公共后缀跟 ...
随机推荐
- 线程入门之sleep
package com.thread; /** * <sleep:使线程休眠一些时间> * <功能详细描述> * * @author 95Yang */ public clas ...
- python_way ,day23 API
python_way ,day23 1.api认证 .api加密动态请求 2.自定义session 一.api认证 首先提供api的公司,如支付宝,微信,都会给你一个用户id,然后还会让你下一个SD ...
- underscore的封装和扩展
// 1. 不污染全局环境 (function() { // 2. 保留之前同名变量 var previousUnderscore = window._; var _ = function(obj) ...
- 硬盘坏道 检测/修复 Windows
1. 主要参看了:http://jingyan.baidu.com/article/2c8c281dfd93df0008252a9b.html 2. 2.1.hdtunepro.zip 是在 http ...
- jQuery的选择器小总结
这一节详细的总结jQuery选择器. 一.基础选择器 $('#info'); // 选择id为info的元素,id为document中是唯一的,因此可以通过该选择器获取唯一的指定元素 $('.in ...
- (四)主控板改IP,升级app,boot,mac
给主控板升级boot要在boot界面进行,进入boot后,要先查看boot下ip和掩码是否和电脑ip(severip)在一个网段,不在的话要使用setenv命令设置ip地址和掩码.之后再输入upboo ...
- ios7适配一些问题以及64位32位
ios7适配一些问题(http://www.cocoachina.com/ios/20130703/6526.html) 1.iOS应用如何实现64位的支持 http://www.codeceo.co ...
- 【浏览器渲染原理】渲染树构建之渲染树和DOM树的关系(转载 学习中。。。)
在DOM树构建的同时,浏览器会构建渲染树(render tree).渲染树的节点(渲染器),在Gecko中称为frame,而在webkit中称为renderer.渲染器是在文档解析和创建DOM节点后创 ...
- maven相关资料
http://www.yiibai.com/maven/ Maven教程 https://www.zhihu.com/question/20104270 http://huangnx.com/tags ...
- Linux 下没有 my.cnf 文件的解决方式,完全是我自己整的,好多教程都是瞎扯的 (zhuan)
http://blog.csdn.net/jspping/article/details/40400691?utm_source=tuicool&utm_medium=referral *** ...