【LG4248】[AHOI2013]差异
【LG4248】[AHOI2013]差异
题面
题解
我们将原串\(reverse\),根据后缀自动机的性质,两个后缀的\(lcp\)一定是我们在反串后两个前缀的\(lca\)。
那么原式不就是求树上两两点对的距离和,
树上一条边的权值可以通过差分求出,就是\(i.len-i.fa.len\)。
然后就统计每一条边的贡献就行了。
感觉少个log效率还跟sa差不多啊
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX_N = 5e5 + 5;
struct Node { int ch[26], len, fa; } t[MAX_N << 1];
int lst = 1, tot = 1, size[MAX_N << 1];
void extend(int c) {
int np = ++tot;
t[lst].ch[c] = np;
t[np].len = t[lst].len + 1;
int p = t[lst].fa; lst = np;
while (p && !t[p].ch[c]) t[p].ch[c] = np, p = t[p].fa;
if (!p) t[np].fa = 1;
else {
int q = t[p].ch[c];
if (t[q].len == t[p].len + 1) t[np].fa = q;
else {
int nq = ++tot;
t[nq] = t[q];
t[nq].len = t[p].len + 1, t[q].fa = t[np].fa = nq;
while (p && t[p].ch[c] == q) t[p].ch[c] = nq, p = t[p].fa;
}
}
size[np] = 1;
}
int N, bln[MAX_N << 1], A[MAX_N << 1];
char a[MAX_N];
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
scanf("%s", a + 1); N = strlen(a + 1);
for (int i = 1; i <= N; i++) extend(a[N - i + 1] - 'a');
for (int i = 1; i <= tot; i++) bln[t[i].len]++;
for (int i = 1; i <= tot; i++) bln[i] += bln[i - 1];
for (int i = 1; i <= tot; i++) A[bln[t[i].len]--] = i;
long long ans = 0;
for (int i = tot; i; i--) {
int x = A[i];
size[t[x].fa] += size[x];
ans += 1ll * (t[x].len - t[t[x].fa].len) * size[x] * (N - size[x]);
}
printf("%lld\n", ans);
return 0;
}
【LG4248】[AHOI2013]差异的更多相关文章
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- bzoj 3238 Ahoi2013 差异
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2357 Solved: 1067[Submit][Status ...
- BZOJ 3238: [Ahoi2013]差异 [后缀自动机]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2512 Solved: 1140[Submit][Status ...
- BZOJ_3238_[Ahoi2013]差异_后缀自动机
BZOJ_3238_[Ahoi2013]差异_后缀自动机 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sam ...
- BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈
BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...
- 【BZOJ3238】[AHOI2013]差异
[BZOJ3238][AHOI2013]差异 题面 给定字符串\(S\),令\(T_i\)表示以它从第\(i\)个字符开始的后缀.求 \[ \sum_{1\leq i<j\leq n}len(T ...
- P4248 [AHOI2013]差异 解题报告
P4248 [AHOI2013]差异 题目描述 给定一个长度为 \(n\) 的字符串 \(S\),令 \(T_i\) 表示它从第 \(i\) 个字符开始的后缀.求 \[\displaystyle \s ...
- 【BZOJ 3238】 3238: [Ahoi2013]差异(SAM)
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 3047 Solved: 1375 Description In ...
- bzoj 3238: [Ahoi2013]差异 -- 后缀数组
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...
随机推荐
- LeetCode题解之Pascal's Triangle II
1.题目描述 2.题目分析 题目要求返回杨辉三角的某一行,需要将杨辉三角的某行的全部计算出来. 3.代码实现 vector<int> getRow(int rowIndex) { ) ,) ...
- Oracle EBS INV 删除保留
DECLARE p_rsv apps.inv_reservation_global.mtl_reservation_rec_type; p_dummy_sn apps.inv_reservation_ ...
- 解决VB6.0中不能加载MSCOMCTL.OCX的错误提示
VB6.0毕竟是很古老的开发工具了,其对所使用的第三方组件依赖性比较强,例如在打开从其它电脑上拿来的VB6.0的软件(系统)的工程文件(源代码)时,经常会遇到"不能加载MSCOMCTL.OC ...
- Unity[C#] Reflection Use
Reflection Reflection是C#程序员的一个最有力工具 最常用的例子来说明反射的用处是一个插件系统.假设你正在创建一个 接受用户创建 的扩展程序,有没有办法预先知道哪些方法这个扩展 ...
- linux stat 简单介绍
stat 命令查看文件或文件系统的状态时间等属性 用法:stat [参数]... 文件... 简单的介绍一下stat命令显示出来的文件其他信息: - File:显示文件名 - Size:显示文件大小 ...
- 深入了解MyBatis二级缓存
深入了解MyBatis二级缓存 标签: mybatis二级缓存 2015-03-30 08:57 41446人阅读 评论(13) 收藏 举报 分类: Mybatis(51) 版权声明:版权归博主所 ...
- python第四十三课——封装性
1.面向对象的三大特性:封装性.继承性.多态性 封装: 封装使用的领悟: 1).生活层面:食品.快递.计算机.明星... 2).计算机层面: ①.模块.类.函数... ②.属性数据的封装与隐藏 权限修 ...
- python第二十九课——文件读写(复制文件)
自定义函数:实现文件复制操作有形参(2个) 没有返回值相似版(不用) def copyFile(src,dest): #1.打开两个文件:1个关联读操作,1个关联写操作 fr=open(src,'rb ...
- swift和oc的protocol的成员变量
都需要协议实现者提供具体变量: 否则认为不符合协议. @protocol edddd <NSObject> @property(nonatomic, strong) NSObject *e ...
- docker 1.13.1 启动容器过程中mount报错
docker 1.13.1 启动container 问题 [root@openfalcon_transfer1 harbor]# docker run -it --name test busybox ...