3238: [Ahoi2013]差异

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 3047  Solved: 1375

Description

Input

一行,一个字符串S

Output

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

Sample Input

cacao

Sample Output

54

HINT

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

Source

【分析】

  这题先把sigma len 加上。

  然后考虑一下减掉的是什么。

  对于每个子串,假设出现次数是x,那么对答案的贡献就是x*(x-1)/2*2即ans-=x*(x-1)。

  这个用SAM对每个点的right进行计算即可。

  当然也可以用后缀数组。【后缀数组要用单调栈吧?套路啊。。

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7. #define Maxn 500010
  8. #define LL long long
  9.  
  10. struct node
  11. {
  12. int son[],pre,step,rt;
  13. // node() {rt=0;}
  14. }t[Maxn*];
  15. int v[Maxn*],q[Maxn*];
  16.  
  17. struct sam
  18. {
  19. int last,tot;
  20. void extend(int k)
  21. {
  22. int np=++tot,p=last;
  23. t[np].step=t[p].step+;
  24. t[np].rt=;
  25. while(p&&!t[p].son[k])
  26. {
  27. t[p].son[k]=np;
  28. p=t[p].pre;
  29. }
  30. if(!p) t[np].pre=;
  31. else
  32. {
  33. int q=t[p].son[k];
  34. if(t[q].step==t[p].step+) t[np].pre=q;
  35. else
  36. {
  37. int nq=++tot;
  38. memcpy(t[nq].son,t[q].son,sizeof(t[nq].son));
  39. t[nq].step=t[p].step+;
  40. t[nq].pre=t[q].pre;
  41. t[q].pre=t[np].pre=nq;
  42. while(p&&t[p].son[k]==q)
  43. {
  44. t[p].son[k]=nq;
  45. p=t[p].pre;
  46. }
  47. }
  48. }
  49. last=np;
  50. }
  51. void init()
  52. {
  53. memset(v,,sizeof(v));
  54. for(int i=;i<=tot;i++) v[t[i].step]++;
  55. for(int i=;i<=tot;i++) v[i]+=v[i-];
  56. for(int i=tot;i>=;i--) q[v[t[i].step]--]=i;
  57.  
  58. for(int i=tot;i>=;i--)
  59. {
  60. int nw=q[i];
  61. t[t[nw].pre].rt+=t[nw].rt;
  62. }
  63. }
  64. }sam;
  65.  
  66. char s[Maxn];
  67.  
  68. int main()
  69. {
  70. LL ans=;
  71. scanf("%s",s);
  72. int l=strlen(s);
  73. sam.last=sam.tot=;
  74. for(int i=;i<l;i++) sam.extend(s[i]-'a'+);
  75. ans=1LL*l*(l+)*(l-)/;
  76. sam.init();t[].step=;
  77. for(int i=;i<=sam.tot;i++)
  78. {
  79. ans-=1LL*(t[i].step-t[t[i].pre].step)*t[i].rt*(t[i].rt-);
  80. }
  81. printf("%lld\n",ans);
  82. return ;
  83. }

2017-04-17 21:06:17

【BZOJ 3238】 3238: [Ahoi2013]差异(SAM)的更多相关文章

  1. luogu P4248 [AHOI2013]差异 SAM

    luogu P4248 [AHOI2013]差异 链接 luogu 思路 \(\sum\limits_{1<=i<j<=n}{{len}(T_i)+{len}(T_j)-2*{lcp ...

  2. 【BZOJ 3238】[Ahoi2013]差异

    [链接]h在这里写链接 [题意]     还有更简洁的题目描述吗/xk [题解]     对于lenti+lentj这一部分,比较好处理.     可以弄一个前缀和.     然后O(N)扫描一遍. ...

  3. BZOJ3238:[AHOI2013]差异(SAM)

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

  4. BZOJ 3238: [Ahoi2013]差异 [后缀自动机]

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

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

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

  6. bzoj 3238 Ahoi2013 差异

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

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

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

  8. BZOJ3238 [Ahoi2013]差异 【SAM or SA】

    BZOJ3238 [Ahoi2013]差异 给定一个串,问其任意两个后缀的最长公共前缀长度的和 1.又是后缀,又是\(lcp\),很显然直接拿\(SA\)的\(height\)数组搞就好了,配合一下单 ...

  9. 笔记-AHOI2013 差异

    AHOI2013 差异 方法1:SA 先板个后缀数组(带 \(height\) 不带 \(st\) 表),用单调队列递推每个后缀 \(sa_i\) 对答案的贡献,求和,用定值减之. #include ...

随机推荐

  1. CSS居中之美

    关于居中,你会想到什么? div{margin: auto;} 常见的居中方法 水平居中 .demo{ text-align: center; margin: auto; position: abso ...

  2. 每个Web开发者都需要具备的9个软技能

    对于一份工作,你可能专注于修炼自己的内功,会在不自觉中忽视软技能.硬技能决定你是否能得到工作,而软技能能够表明你是否适合这份工作和适应工作环境等.所有的公司都有属于自己的文化,并努力将这些文化传承下去 ...

  3. Unity MMO 参考数值

    贴图格式: iOS :RGBA 32 (pvrtc 4 ) Android : RGB Compresed ETC 4 或 RGBA 32  . DrawCall: 总计Drawcall 平均 100 ...

  4. 实现在点击asp:button按钮后,不刷新当前页面

    方法1:return false <asp:Button ID="Button1" runat="server" Text="Button&qu ...

  5. 数据结构与算法之KMP 字符串匹配

    举例来说,有一个字符串"DSFFKFJD KFJLKFDLJFJ IWWJKJFJIA",我想知道,里面是否包含另一个字符串"JFJI",有的话就返回在原字符串 ...

  6. Petrozavodsk Summer Training Camp 2017

    Petrozavodsk Summer Training Camp 2017 Problem A. Connectivity 题目描述:有\(n\)个点,现不断地加边.每条边有一种颜色,如果一个点对\ ...

  7. [转载]理解Tomcat的Classpath-常见问题以及如何解决

    摘自: http://www.linuxidc.com/Linux/2011-08/41684.htm 在很多Apache Tomcat用户论坛,一个问题经常被提出,那就是如何配置Tomcat的cla ...

  8. 利用json模块解析dict报错找不到attribute 'dumps'[python2.7]

    [背景] 环境: RHEL 7.3 版本: python2.7 [错误情况] 写了一个简单的python脚本 将dict转换为json 脚本如下: #!/usr/bin/python #-*- cod ...

  9. LINUX下解决netstat查看TIME_WAIT状态过多问题(转)

    原文连接:www.itokit.com/2012/0516/73950.html # netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c 16 CLOSIN ...

  10. Linux 不常用命令总结

    1. vim编辑模式下,搜索,/user,跳转下一个,小写的n 2.