【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 ...
随机推荐
- CSS居中之美
关于居中,你会想到什么? div{margin: auto;} 常见的居中方法 水平居中 .demo{ text-align: center; margin: auto; position: abso ...
- 每个Web开发者都需要具备的9个软技能
对于一份工作,你可能专注于修炼自己的内功,会在不自觉中忽视软技能.硬技能决定你是否能得到工作,而软技能能够表明你是否适合这份工作和适应工作环境等.所有的公司都有属于自己的文化,并努力将这些文化传承下去 ...
- Unity MMO 参考数值
贴图格式: iOS :RGBA 32 (pvrtc 4 ) Android : RGB Compresed ETC 4 或 RGBA 32 . DrawCall: 总计Drawcall 平均 100 ...
- 实现在点击asp:button按钮后,不刷新当前页面
方法1:return false <asp:Button ID="Button1" runat="server" Text="Button&qu ...
- 数据结构与算法之KMP 字符串匹配
举例来说,有一个字符串"DSFFKFJD KFJLKFDLJFJ IWWJKJFJIA",我想知道,里面是否包含另一个字符串"JFJI",有的话就返回在原字符串 ...
- Petrozavodsk Summer Training Camp 2017
Petrozavodsk Summer Training Camp 2017 Problem A. Connectivity 题目描述:有\(n\)个点,现不断地加边.每条边有一种颜色,如果一个点对\ ...
- [转载]理解Tomcat的Classpath-常见问题以及如何解决
摘自: http://www.linuxidc.com/Linux/2011-08/41684.htm 在很多Apache Tomcat用户论坛,一个问题经常被提出,那就是如何配置Tomcat的cla ...
- 利用json模块解析dict报错找不到attribute 'dumps'[python2.7]
[背景] 环境: RHEL 7.3 版本: python2.7 [错误情况] 写了一个简单的python脚本 将dict转换为json 脚本如下: #!/usr/bin/python #-*- cod ...
- LINUX下解决netstat查看TIME_WAIT状态过多问题(转)
原文连接:www.itokit.com/2012/0516/73950.html # netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c 16 CLOSIN ...
- Linux 不常用命令总结
1. vim编辑模式下,搜索,/user,跳转下一个,小写的n 2.