【BZOJ 3238】 3238: [Ahoi2013]差异(SAM)
3238: [Ahoi2013]差异
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 3047 Solved: 1375Description
Input
一行,一个字符串S
Output
一行,一个整数,表示所求值
Sample Input
cacaoSample Output
54HINT
2<=N<=500000,S由小写英文字母组成
Source
【分析】
这题先把sigma len 加上。
然后考虑一下减掉的是什么。
对于每个子串,假设出现次数是x,那么对答案的贡献就是x*(x-1)/2*2即ans-=x*(x-1)。
这个用SAM对每个点的right进行计算即可。
当然也可以用后缀数组。【后缀数组要用单调栈吧?套路啊。。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 500010
#define LL long long struct node
{
int son[],pre,step,rt;
// node() {rt=0;}
}t[Maxn*];
int v[Maxn*],q[Maxn*]; struct sam
{
int last,tot;
void extend(int k)
{
int np=++tot,p=last;
t[np].step=t[p].step+;
t[np].rt=;
while(p&&!t[p].son[k])
{
t[p].son[k]=np;
p=t[p].pre;
}
if(!p) t[np].pre=;
else
{
int q=t[p].son[k];
if(t[q].step==t[p].step+) t[np].pre=q;
else
{
int nq=++tot;
memcpy(t[nq].son,t[q].son,sizeof(t[nq].son));
t[nq].step=t[p].step+;
t[nq].pre=t[q].pre;
t[q].pre=t[np].pre=nq;
while(p&&t[p].son[k]==q)
{
t[p].son[k]=nq;
p=t[p].pre;
}
}
}
last=np;
}
void init()
{
memset(v,,sizeof(v));
for(int i=;i<=tot;i++) v[t[i].step]++;
for(int i=;i<=tot;i++) v[i]+=v[i-];
for(int i=tot;i>=;i--) q[v[t[i].step]--]=i; for(int i=tot;i>=;i--)
{
int nw=q[i];
t[t[nw].pre].rt+=t[nw].rt;
}
}
}sam; char s[Maxn]; int main()
{
LL ans=;
scanf("%s",s);
int l=strlen(s);
sam.last=sam.tot=;
for(int i=;i<l;i++) sam.extend(s[i]-'a'+);
ans=1LL*l*(l+)*(l-)/;
sam.init();t[].step=;
for(int i=;i<=sam.tot;i++)
{
ans-=1LL*(t[i].step-t[t[i].pre].step)*t[i].rt*(t[i].rt-);
}
printf("%lld\n",ans);
return ;
}
2017-04-17 21:06:17
【BZOJ 3238】 3238: [Ahoi2013]差异(SAM)的更多相关文章
- luogu P4248 [AHOI2013]差异 SAM
luogu P4248 [AHOI2013]差异 链接 luogu 思路 \(\sum\limits_{1<=i<j<=n}{{len}(T_i)+{len}(T_j)-2*{lcp ...
- 【BZOJ 3238】[Ahoi2013]差异
[链接]h在这里写链接 [题意] 还有更简洁的题目描述吗/xk [题解] 对于lenti+lentj这一部分,比较好处理. 可以弄一个前缀和. 然后O(N)扫描一遍. ...
- BZOJ3238:[AHOI2013]差异(SAM)
Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...
- BZOJ 3238: [Ahoi2013]差异 [后缀自动机]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2512 Solved: 1140[Submit][Status ...
- 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 MB Description Input 一行,一个字符串S Output 一行,一个 ...
- BZOJ3238 [Ahoi2013]差异 【SAM or SA】
BZOJ3238 [Ahoi2013]差异 给定一个串,问其任意两个后缀的最长公共前缀长度的和 1.又是后缀,又是\(lcp\),很显然直接拿\(SA\)的\(height\)数组搞就好了,配合一下单 ...
- 笔记-AHOI2013 差异
AHOI2013 差异 方法1:SA 先板个后缀数组(带 \(height\) 不带 \(st\) 表),用单调队列递推每个后缀 \(sa_i\) 对答案的贡献,求和,用定值减之. #include ...
随机推荐
- GridControl详解(七)事件
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventA ...
- 【IIS】IIS中同时满足集成模式和经典模式
手里有一个项目--系统设置(主要功能是对系统一些字典表的设置.权限管理等功能).在VS上运行没有任何问题.可是发布到IIS上之后,报黄页. 发布后程序运行环境为: windows 7 32位 IIS为 ...
- Liunx 下载文件夹下所有文件
136down voteaccepted You may use this in shell: wget -r --no-parent http://abc.tamu.edu/projects/tzi ...
- 【洛谷P1597】语句解析
题目背景 木有背景…… 题目描述 一串(<255)PASCAL语言,只有a,b,c 3个变量,而且只有赋值语句,赋值只能是一个一位的数字或一个变量,未赋值的变量值为0.输出a,b,c 最终的值. ...
- 解决 IE7 中 display:inline-block 失效的问题
我们在做首页菜单选项的时候,通常会用 li 标签去做,通过对 li 标签设置样式: display:inline-block 可以让 li 标签横排显示.但是这样做,在 IE7 浏览器下面会有一个兼容 ...
- bzoj 3083 树链剖分
首先我们先将树提出一个根变成有根树,那么我们可以通过树链剖分来实现对于子树的最小值求解,那么按照当前的根和询问的点的相对位置关系我们可以将询问变成某个子树和或者除去某颗子树之后其余的和,前者直接询问区 ...
- iOS中UITabelView
1.概述 继承自UIScrollView,只能显示一列数据,只能纵向滑动.堪称UIKit里面最复杂的一个控件了,使用起来不算难,但是要用好并不容易.当使用的时候我们必须要考虑到后台数据的设计,tabl ...
- 安装完ODTwithODAC112012,出现ORA-12560:TNS:协议适配器错误
参考:http://blog.csdn.net/tan_yixiu/article/details/6762357 操作系统:windows2008 Enterprise 64位 开发工具:VS201 ...
- fedroa20 没法开启ntpd服务器
1现象:ntpd老是没法开启,ntpd -d显示有个进程占用123端口. [root@vd13crmtb01 ~]# systemctl enable ntpd.service //开 ...
- leetcode 之Search in Rotated Sorted Array(三)
描述 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...