bzoj3238 [Ahoi2013]差异 后缀数组+单调栈
【bzoj3238】[Ahoi2013]差异
Description
Input
一行,一个字符串S
Output
一行,一个整数,表示所求值
Sample Input
Sample Output
题解:
任意两个字符串的lcp是什么,就是如
a,b 那么若a==b 那么为len(a)
否则设sa[a]<sa[b] 那么为min(height[sa[a]+1-------sa[b]])
- #include<cstring>
- #include<iostream>
- #include<cmath>
- #include<algorithm>
- #include<cstdio>
- #define N 500007
- #define ll long long
- using namespace std;
- inline int read()
- {
- int x=,f=;char ch=getchar();
- while(ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
- return x*f;
- }
- int n;
- int stk[N],f[N],g[N];
- struct SA
- {
- char s[N];
- int a[N],b[N],cnta[N],cntb[N],tsa[N],height[N],sa[N],rk[N*];
- void Get_SA()
- {
- for (int i=;i<=;i++)cnta[i]=;
- for (int i=;i<=n;i++)cnta[(int)s[i]]++;
- for (int i=;i<=;i++)cnta[i]+=cnta[i-];
- for (int i=n;i>=;i--)sa[cnta[(int)s[i]]--]=i;
- rk[sa[]]=;
- for (int i=;i<=n;i++)rk[sa[i]]=rk[sa[i-]]+(s[sa[i]]!=s[sa[i-]]);
- for (int i=;rk[sa[n]]!=n;i<<=)
- {
- for (int j=;j<=n;j++)a[j]=rk[j],b[j]=rk[j+i];
- for (int j=;j<=n;j++)cnta[j]=cntb[j]=;
- for (int j=;j<=n;j++)cnta[a[j]]++,cntb[b[j]]++;
- for (int j=;j<=n;j++)cnta[j]+=cnta[j-],cntb[j]+=cntb[j-];
- for (int j=n;j>=;j--)tsa[cntb[b[j]]--]=j;
- for (int j=n;j>=;j--)sa[cnta[a[tsa[j]]]--]=tsa[j];
- rk[sa[]]=;
- for (int j=;j<=n;j++)
- rk[sa[j]]=rk[sa[j-]]+(a[sa[j]]!=a[sa[j-]]||b[sa[j]]!=b[sa[j-]]);
- }
- }
- void Get_Height()
- {
- int len=;
- for (int i=;i<=n;i++)
- {
- if (len)len--;
- while(s[i+len]==s[sa[rk[i]-]+len])len++;
- height[rk[i]]=len;
- }
- }
- }S;
- int main()
- {
- scanf("%s",S.s+);
- n=strlen(S.s+);
- ll ans=;
- for (int i=;i<=n;i++)
- {
- ans+=(ll)(i-)*i;
- ans+=(ll)i*(i-)/;
- }
- S.Get_SA();
- S.Get_Height();
- int tot=;
- for (int i=;i<=n;i++)
- {
- while(tot>&&S.height[i]<S.height[stk[tot]])
- f[stk[tot--]]=i-;
- stk[++tot]=i;
- }
- while(tot)f[stk[tot--]]=n;
- tot=;
- for (int i=n;i>=;i--)
- {
- while(tot>&&S.height[i]<=S.height[stk[tot]])g[stk[tot--]]=i+;
- stk[++tot]=i;
- }
- while(tot)g[stk[tot--]]=;
- for (int i=;i<=n;i++)
- ans-=(ll)S.height[i]*(ll)(f[i]-i+)*(ll)(i-g[i]+)*;
- printf("%lld\n",ans);
- }
bzoj3238 [Ahoi2013]差异 后缀数组+单调栈的更多相关文章
- 【BZOJ-3238】差异 后缀数组 + 单调栈
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1561 Solved: 734[Submit][Status] ...
- 【BZOJ3238】[Ahoi2013]差异 后缀数组+单调栈
[BZOJ3238][Ahoi2013]差异 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Ou ...
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- [AHOI2013] 差异 - 后缀数组,单调栈
[AHOI2013] 差异 Description 求 \(\sum {len(T_i) + len(T_j) - 2 lcp(T_i,T_j)}\) 的值 其中 \(T_i (i = 1,2,... ...
- 【bzoj3238】差异[AHOI2013](后缀数组+单调栈)
题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3238 这道题从大概半年以前就开始啃了,不过当时因为一些细节没调出来,看了Sakits神犇 ...
- [BZOJ3238][AHOI2013]差异(后缀数组)
求和式的前两项可以直接算,问题是对于每对i,j计算LCP. 一个比较显然的性质是,LCP(i,j)是h[rk[i]+1~rk[j]]中的最小值. 从h的每个元素角度考虑,就是对每个h计算有多少对i,j ...
- BZOJ3238 [Ahoi2013]差异 【后缀数组 + 单调栈】
题目链接 BZOJ3238 题解 简单题 经典后缀数组 + 单调栈套路,求所有后缀\(lcp\) #include<iostream> #include<cstdio> #in ...
- BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈
BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...
- bzoj 3238: [Ahoi2013]差异 -- 后缀数组
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...
随机推荐
- GIT分布式版本控制器的前后今生
Git的入门与安装 GIT基础操作 GIT的分支应用 GITLAB应用 gitlab与pycharm应用 GITHUB使用
- HTML5<figure>元素
HTML5<figure>元素是用来定义页面文档中独立的流内容(图像,图表,照片,代码块),figure内容与主内容有关,如果被删除,则不影响主文档流的产生. HTML5<figca ...
- Mutations-freecodecamp算法题目
Mutations(比较字符串) 要求 如果数组第一个字符串元素包含了第二个字符串元素的所有字符,函数返回true. 不用考虑大小写和字符顺序 思路 将数组中的两个字符串小写化 将第二个数组元素(第二 ...
- 【点分治】luoguP2664 树上游戏
应该是一道中等难度的点分?麻烦在一些细节. 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] ...
- Python爬虫系列-PyQuery详解
强大又灵活的网页解析库.如果你觉得正则写起来太麻烦,如果你觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,那么PyQuery就是你的最佳选择. 安装 pip3 install ...
- java实现验证码功能
java实现验证码功能 通过java代码实现验证码功能的一般思路: 一.通过java代码生成一张验证码的图片,将验证码的图片保存到项目中的指定文件中去,代码如下: package com.util; ...
- LeetCode之Weekly Contest 92
第一题:转置矩阵 问题: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9] ...
- Gym - 100781A Adjoin the Networks (树的直径)
题意: n个点,m条边,m <= n <= 100000,边的长度都为1. 点从 0 ~ n-1 编号.开始时图是不连通的,并且没有环. 通过加入一些边后,可以使图连通.要求加入的边不能多 ...
- 水题:CF16C-Monitor
Monitor 题目描述 Reca company makes monitors, the most popular of their models is AB999 with the screen ...
- MySQL使用yum安装
1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2.安装mysql-comm ...