思路

求本质不同的子串个数,总共重叠的子串个数就是height数组的和

总子串个数-height数组的和即可

代码

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #define int long long
  5. const int MAXN = 100000;
  6. using namespace std;
  7. int height[MAXN],sa[MAXN],ranks[MAXN],barrel[MAXN],n;
  8. struct Node{
  9. int pos,r[2];
  10. }midx[MAXN],x[MAXN];
  11. char s[MAXN];
  12. int c_sort(int n,int lim){
  13. for(int i=0;i<2;i++){
  14. memset(barrel,0,sizeof(barrel));
  15. for(int j=1;j<=n;j++)
  16. barrel[x[j].r[i]]++;
  17. for(int j=1;j<=lim;j++)
  18. barrel[j]+=barrel[j-1];
  19. for(int j=n;j>=1;j--)
  20. midx[barrel[x[j].r[i]]--]=x[j];
  21. for(int j=1;j<=n;j++)
  22. x[j]=midx[j];
  23. }
  24. ranks[x[1].pos]=1;
  25. int cnt=1;
  26. for(int i=2;i<=n;i++){
  27. if(x[i].r[0]==x[i-1].r[0]&&x[i].r[1]==x[i-1].r[1])
  28. ranks[x[i].pos]=cnt;
  29. else
  30. ranks[x[i].pos]=++cnt;
  31. }
  32. return cnt;
  33. }
  34. void cal_sa(int n){
  35. for(int i=1;i<=n;i++)
  36. x[i]=(Node){i,s[i],0};
  37. int cnt=c_sort(n,1500);
  38. for(int i=1;cnt<n;i<<=1){
  39. for(int j=1;j<=n;j++)
  40. x[j]=(Node){j,(i+j<=n)?ranks[i+j]:0,ranks[j]};
  41. cnt=c_sort(n,cnt);
  42. }
  43. for(int i=1;i<=n;i++)
  44. sa[ranks[i]]=i;
  45. for(int i=1,j=0,k;i<=n;height[ranks[i++]]=j)
  46. for(j?j--:0,k=sa[ranks[i]-1];s[i+j]==s[j+k];j++);
  47. }
  48. void init(void){
  49. memset(sa,0,sizeof(sa));
  50. memset(height,0,sizeof(height));
  51. memset(ranks,0,sizeof(ranks));
  52. memset(x,0,sizeof(x));
  53. memset(midx,0,sizeof(midx));
  54. }
  55. signed main(){
  56. int T;
  57. scanf("%lld",&T);
  58. while(T--){
  59. init();
  60. scanf("%s",s+1);
  61. n=strlen(s+1);
  62. cal_sa(n);
  63. int sum=0;
  64. for(int i=2;i<=n;i++)
  65. sum+=height[i];
  66. printf("%lld\n",(n+1)*n/2-sum);
  67. }
  68. return 0;
  69. }

SPOJ 694 DISUBSTR - Distinct Substrings的更多相关文章

  1. SPOJ 694&&SPOJ705: Distinct Substrings

    DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...

  2. 【SPOJ 694】Distinct Substrings 不相同的子串的个数

    不会FQ啊,没法评测啊,先存一下代码QAQ 2016-06-16神犇Menci帮我测过AC了,谢谢神犇Menci QwQ #include<cstdio> #include<cstr ...

  3. 【SPOJ 694】Distinct Substrings (更直接的求法)

    [链接]h在这里写链接 [题意] 接上一篇文章 [题解] 一个字符串所有不同的子串的个数=∑(len-sa[i]-height[i]) [错的次数] 0 [反思] 在这了写反思 [代码] #inclu ...

  4. 【SPOJ 694】Distinct Substrings

    [链接]h在这里写链接 [题意]     给你一个长度最多为1000的字符串     让你求出一个数x,这个x=这个字符串的不同子串个数; [题解]     后缀数组题.     把原串复制一份,加在 ...

  5. SPOJ 694 || 705 Distinct Substrings ( 后缀数组 && 不同子串的个数 )

    题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀 ...

  6. DISUBSTR - Distinct Substrings

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  7. SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)

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

  8. SPOJ - DISUBSTR Distinct Substrings (后缀数组)

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

  9. 后缀数组:SPOJ SUBST1 - New Distinct Substrings

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

随机推荐

  1. clientWidth,offsetWidth,scrollWidth区别

    <html> <head> <title>clientWidth,offsetWidth,scrollWidth区别</title> </head ...

  2. word2vector 资料

    http://blog.csdn.net/garfielder007/article/details/51345201 https://cs224d.stanford.edu/lecture_note ...

  3. jQuery筛选--first()和last()

       first() 概述 获取匹配的第一个元素    last() 概述 获取匹配的最后个元素 <!DOCTYPE html> <html> <head> < ...

  4. QtCreator 调试源码

    [1]安装源码 声明:要想调试进入Qt源码,必须首先保证我们安装了Qt源码.下面说明安装Qt源码注意事项. 一般安装过程(默认不安装源码): 安装源码过程(需要自己设置,点击“全选”): 综上所述:Q ...

  5. Python使用闭包结合配置自动生成函数

    背景 在构建测试用例集时,常常需要编写一些函数,这些函数接受基本相同的参数,仅有一个参数有所差异,并且处理模式也非常相同.可以使用Python闭包来定义模板函数,然后通过参数调节来自动化生产不同的函数 ...

  6. Saiku + Kylin 多维分析平台探索

    背景 为了应对各种数据需求,通常,我们的做法是这样的: 对于临时性的数据需求:写HQL到Hive里去查一遍,然后将结果转为excel发送给需求人员. 对于周期性的.长期性的数据需求:编写脚本,结合Hi ...

  7. Spring boot 问题总结

    1. Spring boot 嵌入的tomcat不能启动: Unregistering JMX-exposed beans on shutdown   在官网(http://start.spring. ...

  8. Spring整合Shiro

    apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的 认证和授权方式.其提供的 native-session(即把用户认证后的授权信息保存 ...

  9. 转:Image与byte之间互转

    #region<Image 与 byte之间互转> /// <summary> /// 将一个byte转换成一个Bitmap对象 /// </summary> // ...

  10. Vuejs vm对象详解

    Vuejs vm对象详解 vue数据是怎么驱动视图的?一堆数据放在那里是不会有任何作用的,它必须通过我们的View Model(视图模型)才能操控视图. 图中的model其实就是数据,一般我们写成js ...