求不重复的子串个数

用所有的减去height就好了 推出来的。。。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <sstream>
  4. #include <cstring>
  5. #include <map>
  6. #include <cctype>
  7. #include <set>
  8. #include <vector>
  9. #include <stack>
  10. #include <queue>
  11. #include <algorithm>
  12. #include <cmath>
  13. #define rap(i, a, n) for(int i=a; i<=n; i++)
  14. #define rep(i, a, n) for(int i=a; i<n; i++)
  15. #define lap(i, a, n) for(int i=n; i>=a; i--)
  16. #define lep(i, a, n) for(int i=n; i>a; i--)
  17. #define rd(a) scanf("%d", &a)
  18. #define rlld(a) scanf("%lld", &a)
  19. #define rc(a) scanf("%c", &a)
  20. #define rs(a) scanf("%s", a)
  21. #define MOD 2018
  22. #define LL long long
  23. #define ULL unsigned long long
  24. #define Pair pair<int, int>
  25. #define mem(a, b) memset(a, b, sizeof(a))
  26. #define _ ios_base::sync_with_stdio(0),cin.tie(0)
  27. //freopen("1.txt", "r", stdin);
  28. using namespace std;
  29. const int maxn = , INF = 0x7fffffff;
  30.  
  31. char a[maxn];
  32. int s[maxn];
  33. int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
  34. int ran[maxn], height[maxn];
  35.  
  36. void get_sa(int m)
  37. {
  38. int i, *x = t, *y = t2;
  39. for(i = ; i < m; i++) c[i] = ;
  40. for(i = ; i < n; i++) c[x[i] = s[i]]++;
  41. for(i = ; i < m; i++) c[i] += c[i-];
  42. for(i = n-; i >= ; i--) sa[--c[x[i]]] = i;
  43. for(int k = ; k <= n; k <<= )
  44. {
  45. int p = ;
  46. for(i = n-k; i < n; i++) y[p++] = i;
  47. for(i = ; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
  48. for(i = ; i < m; i++) c[i] = ;
  49. for(i = ; i < n; i++) c[x[y[i]]]++;
  50. for(i = ; i< m; i++) c[i] += c[i-];
  51. for(i = n-; i >= ; i--) sa[--c[x[y[i]]]] = y[i];
  52. swap(x, y);
  53. p = ; x[sa[]] = ;
  54. for(i = ; i < n; i++)
  55. x[sa[i]] = y[sa[i-]] == y[sa[i]] && y[sa[i-]+k] == y[sa[i]+k] ? p- : p++;
  56. if(p >= n) break;
  57. m = p;
  58. }
  59. int k = ;
  60. for(i = ; i < n; i++) ran[sa[i]] = i;
  61. for(i = ; i < n; i++)
  62. {
  63. if(k) k--;
  64. int j = sa[ran[i]-];
  65. while(s[i+k] == s[j+k]) k++;
  66. height[ran[i]] = k;
  67. }
  68. }
  69.  
  70. int main()
  71. {
  72. int T;
  73. rd(T);
  74. while(T--)
  75. {
  76. rs(a);
  77. n = strlen(a);
  78. rep(i, , n)
  79. s[i] = a[i];
  80. int sum = n*(n+)/;
  81. s[n++] = ;
  82. get_sa();
  83. rep(i, , n)
  84. sum -= height[i];
  85. cout<< sum <<endl;
  86.  
  87. }
  88.  
  89. return ;
  90. }

Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)的更多相关文章

  1. Distinct Substrings SPOJ - DISUBSTR 后缀数组

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  2. Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)

    Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...

  3. 705. New Distinct Substrings spoj(后缀数组求所有不同子串)

    705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...

  4. SPOJ DISUBSTR ——后缀数组

    [题目分析] 后缀数组模板题. 由于height数组存在RMQ的性质. 那么对于一个后缀,与前面相同的串总共有h[i]+sa[i]个.然后求和即可. [代码](模板来自Claris,这个板子太漂亮了) ...

  5. SPOJ DISUBSTR 后缀数组

    题目链接:http://www.spoj.com/problems/DISUBSTR/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字 ...

  6. [spoj DISUBSTR]后缀数组统计不同子串个数

    题目链接:https://vjudge.net/contest/70655#problem/C 后缀数组的又一神奇应用.不同子串的个数,实际上就是所有后缀的不同前缀的个数. 考虑所有的后缀按照rank ...

  7. poj2774(后缀数组水题)

    http://poj.org/problem?id=2774 题意:给你两串字符,要你找出在这两串字符中都出现过的最长子串......... 思路:先用个分隔符将两个字符串连接起来,再用后缀数组求出h ...

  8. BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题

    BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题 将字符串复制一遍接在原串后面,然后后缀排序即可. #include <cmath> #include &l ...

  9. POJ 2774 Long Long Message 后缀数组模板题

    题意 给定字符串A.B,求其最长公共子串 后缀数组模板题,求出height数组,判断sa[i]与sa[i-1]是否分属字符串A.B,统计答案即可. #include <cstdio> #i ...

随机推荐

  1. hdu1754 I Hate It(线段树单点更新,区间查询)

    传送门 有更新单个学生成绩和查询某个区间内学生成绩最大值两种操作 线段树代码 #include<bits/stdc++.h> using namespace std; +; using n ...

  2. Android studio Error occurred during initialization of VM 问题解决

    最近开发导入其他Android项目遇见的问题,如下图: 解决办法: 将org.gradle.jvmargs=的值该为521(堆内存分配过高导致) 备忘,希望能帮助到大家

  3. 高速下载百度网盘的方法,只需要一个软件【win/mac/linux】

    论坛里差不多全部都是用百度云的,但是官方百度云的限速,大家都懂的,下面教大家使用一个软件满速下载,不用油猴 1,软件支持mac,win64/32,linux.Linux只有自己编译使用,我也不会哈哈哈 ...

  4. [linux] lsyncd同步工具

    环境说明: 192.168.56.101 同步源 192.168.56.102 同步目标 操作系统centos 7 lsyncd项目地址:https://github.com/axkibe/lsync ...

  5. url的param与dict转换

    urllib.parse.urlencode urlencode from urllib import parse from urllib.request import urlopen from ur ...

  6. 看oracle的sid

    ps -ef|grep pmon 可以从进程名字里看到 也可以通过 sqlplus / as sysdbashow parameter instance_name

  7. 第十二次作业psp

    psp 进度条 代码累积折线图 博文累积折线图 psp饼状图

  8. 20172319 2018.03.27-04.05 《Java程序设计》第4周学习总结

    20172319 2018.03.27-04.05 <Java程序设计>第4周学习总结 教材学习内容总结 第四章 编写类 类与对象的回顾:对象是有状态的,状态由对象的属性值确定.属性由类中 ...

  9. 利用JAVA制作简单登录窗口

    import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...

  10. 福大软工1816 · 评分结果 · Alpha冲刺

    作业地址:alpha冲刺1.alpha冲刺2.alpha冲刺3.alpha冲刺4.alpha冲刺5.alpha冲刺6.alpha冲刺7.alpha冲刺8.alpha冲刺9.alpha冲刺10 作业提交 ...