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树上面两个串的最长公共后缀跟 ...
随机推荐
- git bash下的选择、复制、粘贴
1. 打开git bash 2.点击左上角,选择属性,打钩 3.回到界面,选择一行文字,然后点击 “右键”,这样就复制到剪切板了.再点 “右键”,可以粘贴到命令窗口中了.
- HDU 5929 Basic Data Structure 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- spring集成quartz scheduler
创建项目 有两种创建quart配置作业 1.使用MethodInvokingJobDetailFactoryBean Quartz Scheduler 配置作业(MethodInvokingJobD ...
- php提示:Call to undefined function curl_init
我要利用curl函数进行数据采集时发现提示Call to undefined function curl_init错误了,后来从官网了解到因为curl默认不是php开启的函数,我们需要手工打开哦,下面 ...
- Activtiy
Class Overview An activity is a single, focused thing that the user can do. Almost all activities in ...
- html的空格和换行显示【摘自网络】
一.HTML 代码中的所有连续的空格或空行(换行)都会被显示为一个空格,不管是内容还是标签之间. 二.当我们想让它们在同一行连续显示时,就让所有的代码之间没有空格,也不要换行. 三.当我们想要显示连续 ...
- nginx的location root 指令
原文:http://blog.csdn.net/bjash/article/details/8596538 location /img/ { alias /var/www/image/; } #若按照 ...
- Vbs脚本经典教材(转)
Vbs脚本经典教材(最全的资料还是MSDN) —为什么要使用Vbs? 在Windows中,学习计算机操作也许很简单,但是很多计算机工作是重复性劳动,例如你每周也许需要对一些计算机文件进行复制.粘贴.改 ...
- springside3.1.8打包
地址寻找 https://sourceforge.net/ https://sourceforge.net/projects/springside/files/SpringSide%203.0/3.3 ...
- 测序深度和覆盖度(Sequencing depth and coverage)
总是跑数据,却对数据一无所知,这说不过去吧. 看几篇文章吧 Sequencing depth and coverage: key considerations in genomic analyses( ...