3238: [Ahoi2013]差异

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 2106  Solved: 953
[Submit][Status][Discuss]

Description

Input

一行,一个字符串S

Output

一行,一个整数,表示所求值

Sample Input

cacao

Sample Output


54

HINT

2<=N<=500000,S由小写英文字母组成

Source

[Submit][Status][Discuss]

构造一个后缀数组,两个后缀的lcp就是在这个数组走过去时取min,单调队列瞎搞即可 写这个真的想吐。。。

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;const int maxn = 5E5 + 50;
typedef long long LL;int n,m,co[maxn],t[maxn],t2[maxn],height[maxn],l[maxn],r[maxn],q[maxn],rank[maxn],sa[maxn];
char ch[maxn];
LL ans = 0;void Getsa()
{int *x = t,*y = t2; m = 26;for (int i = 1; i <= n; i++) ++co[x[i] = ch[i]];for (int i = 2; i <= m; i++) co[i] += co[i-1]; for (int i = n; i; i--) sa[co[x[i]]--] = i; for (int k = 1; k < n; k <<= 1) {int p = 0;for (int i = n; i > n - k; i--) y[++p] = i;for (int i = 1; i <= n; i++) if (sa[i] - k > 0) y[++p] = sa[i] - k;for (int i = 1; i <= m; i++) co[i] = 0;for (int i = 1; i <= n; i++) ++co[x[y[i]]];for (int i = 2; i <= m; i++) co[i] += co[i-1];for (int i = n; i; i--) sa[co[x[y[i]]]--] = y[i];swap(x,y); x[sa[1]] = (p = 1);for (int i = 2; i <= n; i++)x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]?p:++p;if (p == n) break;m = p; }
}void Rank_and_Height()
{for (int i = 1; i <= n; i++) rank[sa[i]] = i;int k = 0;for (int i = 1; i <= n; i++) {if (k) --k;int j = sa[rank[i]-1];while (ch[i + k] == ch[j + k]) ++k;height[rank[i]] = k;}
}int main()
{#ifdef DMCfreopen("DMC.txt","r",stdin);#endifscanf("%s",ch + 1); n = strlen(ch + 1);for (int i = 1; i <= n; i++) ch[i] = ch[i] - 'a' + 1;Getsa(); Rank_and_Height();int tail = 0; q[tail = 1] = 0;for (int i = 1; i <= n + 1; i++) {while (tail && height[q[tail]] > height[i]) r[q[tail--]] = i-1;q[++tail] = i;}while (tail) r[q[tail--]] = n;q[tail = 1] = n + 1;for (int i = n; i >= 0; i--) {while (tail && height[q[tail]] >= height[i]) l[q[tail--]] = i+1;q[++tail] = i;}while (tail) l[q[tail--]] = 1;for (int i = 2; i <= n; i++) ans -= 2LL*height[i]*(i-l[i]+1LL)*(r[i]-i+1LL);LL K = 1LL*(1+n)*n/2LL;cout << K*1LL*(n-1LL) + ans;return 0;
}

3238: [Ahoi2013]差异相关推荐

  1. BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 2326  Solved: 1054 [Submit][Stat ...

  2. bzoj 3238: [Ahoi2013]差异(后缀数组+单调栈)

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 3443  Solved: 1562 [Submit][Stat ...

  3. 【BZOJ】 3238: [Ahoi2013]差异

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3238 求:$${\sum _{i=1}^{n-1}\sum _{j=i+1}^{n}len ...

  4. @bzoj - 3238@ [Ahoi2013]差异

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个长度为 n 的字符串 S,令 Ti 表示它从第 i 个字 ...

  5. bzoj 3238: [Ahoi2013]差异

    Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...

  6. 【BZOJ】3238: [Ahoi2013]差异

    [题意]给定长度为n的小写字母字符串,令Ti表示以i开头的后缀,求Σ[Ti+Tj-2*lcp(Ti,Tj)],1<=i<j<=n. [算法]后缀自动机 [题解]Σ(Ti+Tj)只与n ...

  7. BZOJ 3238 [Ahoi2013]差异

    后缀自动机+后缀树 有一种不难想到的后缀数组的做法,详见黄学长题解 然而我做这题的目的主要是熟悉一下后缀自动机和后缀树- - 一个有趣的性质:反串的后缀自动机的parent树就是正串的后缀树,其树上的 ...

  8. BZOJ3238: [Ahoi2013]差异

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 4840  Solved: 2298 [Submit][Stat ...

  9. 【bzoj3238】[Ahoi2013]差异

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 3425  Solved: 1559 [Submit][Stat ...

最新文章

  1. Oracle 10g R2 RAC手动打补丁PSU(10.2.0.5.19)
  2. 火狐浏览器插件_期待!全新安卓正式版Firefox火狐浏览器,将支持97个插件
  3. boost::math模块使用逆高斯(或逆正态)分布的示例
  4. 分块查询 缓解内存开销
  5. c++简单实现http协议服务器和客户端
  6. php的用户认证(有点难度 多看几遍吧)
  7. 启明星辰集团:文化筑底,战略引领信息安全之路
  8. iOS tableview嵌套collectionview
  9. DataRowView 笔记
  10. latex生成的pdf论文在打印时页码混乱的解决方法
  11. HBase权威指南阅读——第一章
  12. python数据分析与展示 嵩天_【学习笔记】PYTHON数据分析与展示(北理工 嵩天)
  13. Python3网络爬虫(九):使用Selenium爬取百度文库word文章
  14. 程控电源测试使用小记
  15. 大反转!马斯克哭求被裁员工回来
  16. Riot Game前高管:游戏玩家将成为Web3真正粉丝的15大原因
  17. 安卓设备脱离pc自动化测试,Termux模拟器下python+uiautomator2的环境设置。本人亲测,踩坑总结。
  18. yana--Flutter一些学习
  19. HTML设置网站标题前的logo
  20. LQR控制器——简单实现与仿真

热门文章

  1. python计算机视觉——立体匹配与NCC算法
  2. Synergistic Saliency and Depth Prediction for RGB-D Saliency Detection
  3. ERROR: Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately
  4. H3C服务器重做RAID
  5. CC2640R2F BLE5.0 CC2640R2F UART驱动
  6. matlab滤波器滤除低频直流信号,对低频信号的滤波的方法
  7. FPGA error:buffers of the same direction cannot be placed in series.
  8. JS base64编码解码实战
  9. samba服务器的功能
  10. XSS-labs靶场通关秘籍(level 17-20)