【bzoj3238】[Ahoi2013]差异

Description

Input

一行,一个字符串S

Output

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

Sample Input

cacao

Sample Output

54

题解:

任意两个字符串的lcp是什么,就是如

a,b  那么若a==b 那么为len(a)

  否则设sa[a]<sa[b] 那么为min(height[sa[a]+1-------sa[b]])

  1. #include<cstring>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstdio>
  6.  
  7. #define N 500007
  8. #define ll long long
  9. using namespace std;
  10. inline int read()
  11. {
  12. int x=,f=;char ch=getchar();
  13. while(ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
  14. while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
  15. return x*f;
  16. }
  17.  
  18. int n;
  19. int stk[N],f[N],g[N];
  20. struct SA
  21. {
  22. char s[N];
  23. int a[N],b[N],cnta[N],cntb[N],tsa[N],height[N],sa[N],rk[N*];
  24. void Get_SA()
  25. {
  26. for (int i=;i<=;i++)cnta[i]=;
  27. for (int i=;i<=n;i++)cnta[(int)s[i]]++;
  28. for (int i=;i<=;i++)cnta[i]+=cnta[i-];
  29. for (int i=n;i>=;i--)sa[cnta[(int)s[i]]--]=i;
  30. rk[sa[]]=;
  31. for (int i=;i<=n;i++)rk[sa[i]]=rk[sa[i-]]+(s[sa[i]]!=s[sa[i-]]);
  32. for (int i=;rk[sa[n]]!=n;i<<=)
  33. {
  34. for (int j=;j<=n;j++)a[j]=rk[j],b[j]=rk[j+i];
  35. for (int j=;j<=n;j++)cnta[j]=cntb[j]=;
  36. for (int j=;j<=n;j++)cnta[a[j]]++,cntb[b[j]]++;
  37. for (int j=;j<=n;j++)cnta[j]+=cnta[j-],cntb[j]+=cntb[j-];
  38. for (int j=n;j>=;j--)tsa[cntb[b[j]]--]=j;
  39. for (int j=n;j>=;j--)sa[cnta[a[tsa[j]]]--]=tsa[j];
  40. rk[sa[]]=;
  41. for (int j=;j<=n;j++)
  42. rk[sa[j]]=rk[sa[j-]]+(a[sa[j]]!=a[sa[j-]]||b[sa[j]]!=b[sa[j-]]);
  43. }
  44. }
  45. void Get_Height()
  46. {
  47. int len=;
  48. for (int i=;i<=n;i++)
  49. {
  50. if (len)len--;
  51. while(s[i+len]==s[sa[rk[i]-]+len])len++;
  52. height[rk[i]]=len;
  53. }
  54. }
  55. }S;
  56. int main()
  57. {
  58. scanf("%s",S.s+);
  59. n=strlen(S.s+);
  60. ll ans=;
  61. for (int i=;i<=n;i++)
  62. {
  63. ans+=(ll)(i-)*i;
  64. ans+=(ll)i*(i-)/;
  65. }
  66. S.Get_SA();
  67. S.Get_Height();
  68. int tot=;
  69. for (int i=;i<=n;i++)
  70. {
  71. while(tot>&&S.height[i]<S.height[stk[tot]])
  72. f[stk[tot--]]=i-;
  73. stk[++tot]=i;
  74. }
  75. while(tot)f[stk[tot--]]=n;
  76. tot=;
  77. for (int i=n;i>=;i--)
  78. {
  79. while(tot>&&S.height[i]<=S.height[stk[tot]])g[stk[tot--]]=i+;
  80. stk[++tot]=i;
  81. }
  82. while(tot)g[stk[tot--]]=;
  83. for (int i=;i<=n;i++)
  84. ans-=(ll)S.height[i]*(ll)(f[i]-i+)*(ll)(i-g[i]+)*;
  85. printf("%lld\n",ans);
  86. }

bzoj3238 [Ahoi2013]差异 后缀数组+单调栈的更多相关文章

  1. 【BZOJ-3238】差异 后缀数组 + 单调栈

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1561  Solved: 734[Submit][Status] ...

  2. 【BZOJ3238】[Ahoi2013]差异 后缀数组+单调栈

    [BZOJ3238][Ahoi2013]差异 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Ou ...

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

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

  4. [AHOI2013] 差异 - 后缀数组,单调栈

    [AHOI2013] 差异 Description 求 \(\sum {len(T_i) + len(T_j) - 2 lcp(T_i,T_j)}\) 的值 其中 \(T_i (i = 1,2,... ...

  5. 【bzoj3238】差异[AHOI2013](后缀数组+单调栈)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3238 这道题从大概半年以前就开始啃了,不过当时因为一些细节没调出来,看了Sakits神犇 ...

  6. [BZOJ3238][AHOI2013]差异(后缀数组)

    求和式的前两项可以直接算,问题是对于每对i,j计算LCP. 一个比较显然的性质是,LCP(i,j)是h[rk[i]+1~rk[j]]中的最小值. 从h的每个元素角度考虑,就是对每个h计算有多少对i,j ...

  7. BZOJ3238 [Ahoi2013]差异 【后缀数组 + 单调栈】

    题目链接 BZOJ3238 题解 简单题 经典后缀数组 + 单调栈套路,求所有后缀\(lcp\) #include<iostream> #include<cstdio> #in ...

  8. BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈

    BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...

  9. bzoj 3238: [Ahoi2013]差异 -- 后缀数组

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...

随机推荐

  1. GIT分布式版本控制器的前后今生

    Git的入门与安装 GIT基础操作 GIT的分支应用 GITLAB应用 gitlab与pycharm应用 GITHUB使用

  2. HTML5<figure>元素

    HTML5<figure>元素是用来定义页面文档中独立的流内容(图像,图表,照片,代码块),figure内容与主内容有关,如果被删除,则不影响主文档流的产生. HTML5<figca ...

  3. Mutations-freecodecamp算法题目

    Mutations(比较字符串) 要求 如果数组第一个字符串元素包含了第二个字符串元素的所有字符,函数返回true. 不用考虑大小写和字符顺序 思路 将数组中的两个字符串小写化 将第二个数组元素(第二 ...

  4. 【点分治】luoguP2664 树上游戏

    应该是一道中等难度的点分?麻烦在一些细节. 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] ...

  5. Python爬虫系列-PyQuery详解

    强大又灵活的网页解析库.如果你觉得正则写起来太麻烦,如果你觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,那么PyQuery就是你的最佳选择. 安装 pip3 install ...

  6. java实现验证码功能

    java实现验证码功能 通过java代码实现验证码功能的一般思路: 一.通过java代码生成一张验证码的图片,将验证码的图片保存到项目中的指定文件中去,代码如下: package com.util; ...

  7. LeetCode之Weekly Contest 92

    第一题:转置矩阵 问题: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9] ...

  8. Gym - 100781A Adjoin the Networks (树的直径)

    题意: n个点,m条边,m <= n <= 100000,边的长度都为1. 点从 0 ~ n-1 编号.开始时图是不连通的,并且没有环. 通过加入一些边后,可以使图连通.要求加入的边不能多 ...

  9. 水题:CF16C-Monitor

    Monitor 题目描述 Reca company makes monitors, the most popular of their models is AB999 with the screen ...

  10. MySQL使用yum安装

    1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2.安装mysql-comm ...