DISUBSTR - Distinct Substrings

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

Input

T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 1000

Output

For each test case output one number saying the number of distinct substrings.

Example

Sample Input:
2
CCCCC
ABABA

Sample Output:
5
9

Explanation for the testcase with string ABABA: 
len=1 : A,B
len=2 : AB,BA
len=3 : ABA,BAB
len=4 : ABAB,BABA
len=5 : ABABA
Thus, total number of distinct substrings is 9.

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. const int MAXN=;
  6. char buf[MAXN];
  7. int sa[MAXN];
  8. int rnk[MAXN];
  9. int tmp[MAXN];
  10. int lcp[MAXN];
  11. int len,k;
  12.  
  13. bool comp(int i,int j)
  14. {
  15. if(rnk[i]!=rnk[j]) return rnk[i]<rnk[j];
  16. else{
  17. int ri=(i+k<=len)?rnk[i+k]:-;
  18. int rj=(j+k<=len)?rnk[j+k]:-;
  19. return ri<rj;
  20. }
  21. }
  22.  
  23. void getsa()
  24. {
  25. memset(rnk,,sizeof(rnk));
  26. memset(sa,,sizeof(sa));
  27. memset(tmp,,sizeof(tmp));
  28.  
  29. len=strlen(buf);
  30. for(int i=;i<len;i++)
  31. {
  32. sa[i]=i;
  33. rnk[i]=buf[i]-'A';
  34. }
  35. sa[len]=len;
  36. rnk[len]=-;
  37.  
  38. for(k=;k<=len;k*=)
  39. {
  40. sort(sa,sa+len+,comp);
  41.  
  42. tmp[sa[]]=;
  43. for(int i=;i<=len;i++)
  44. {
  45. tmp[sa[i]]=tmp[sa[i-]]+(comp(sa[i-],sa[i])?:);
  46. }
  47.  
  48. for(int i=;i<=len;i++)
  49. {
  50. rnk[i]=tmp[i];
  51. }
  52. }
  53. }
  54.  
  55. void getlcp()
  56. {
  57. memset(rnk,,sizeof(rnk));
  58. memset(lcp,,sizeof(lcp));
  59. getsa();
  60. for(int i=;i<=len;i++)
  61. {
  62. rnk[sa[i]]=i;
  63. }
  64.  
  65. int h=;
  66. lcp[]=h;
  67. for(int i=;i<len;i++)
  68. {
  69. int j=sa[rnk[i]-];
  70. if(h>) h--;
  71. for(;h+i<len&&h+j<len;h++)
  72. if(buf[i+h]!=buf[j+h]) break;
  73. lcp[rnk[i]-]=h;
  74. }
  75. }
  76.  
  77. void debug()
  78. {
  79. for(int i=;i<=len;i++)
  80. {
  81. int l=sa[i];
  82. if(l==len)
  83. {
  84. printf("%d %d\n",l,lcp[i]);
  85. }
  86. else
  87. {
  88. for(int j=l;j<len;j++)
  89. printf("%c ",buf[j]);
  90. printf("%d\n",lcp[i]);
  91. }
  92. }
  93.  
  94. }
  95. int T;
  96. int main()
  97. {
  98. scanf("%d",&T);
  99. while(T--)
  100. {
  101. scanf("%s",buf);
  102. int res=;
  103. getlcp();
  104. res+=(len+)*len/;
  105. for(int i=;i<=len;i++)
  106. res-=lcp[i];
  107. printf("%d\n",res);
  108. }
  109.  
  110. return ;
  111. }

SPOJ(后缀数组求不同子串个数)的更多相关文章

  1. SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

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

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

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

  3. SPOJ SUBST1 New Distinct Substrings(后缀数组 本质不同子串个数)题解

    题意: 问给定串有多少本质不同的子串? 思路: 子串必是某一后缀的前缀,假如是某一后缀\(sa[k]\),那么会有\(n - sa[k] + 1\)个前缀,但是其中有\(height[k]\)个和上一 ...

  4. spoj 694. Distinct Substrings 后缀数组求不同子串的个数

    题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...

  5. spoj705 后缀数组求不同子串的个数

    http://www.spoj.com/problems/SUBST1/en/  题目链接 SUBST1 - New Distinct Substrings no tags  Given a stri ...

  6. poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14874   Accepted: 5118 De ...

  7. 【POJ2774】Long Long Message(后缀数组求Height数组)

    点此看题面 大致题意: 求两个字符串中最长公共子串的长度. 关于后缀数组 关于\(Height\)数组的概念以及如何用后缀数组求\(Height\)数组详见这篇博客:后缀数组入门(二)--Height ...

  8. poj 1743 后缀数组 求最长不重叠重复子串

    题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...

  9. SPOJ REPEATS Repeats (后缀数组 + RMQ:子串的最大循环节)题解

    题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[ ...

随机推荐

  1. Android HandlerThread 消息循环机制之源代码解析

    关于 HandlerThread 这个类.可能有些人眼睛一瞟,手指放在键盘上,然后就是一阵狂敲.立即就能敲出一段段华丽的代码: HandlerThread handlerThread = new Ha ...

  2. JD笔试试题(凭记忆写的+人生感悟 try finally )

    京东笔试:技术篇(一套卷.包含測试.算法,研发) 一:填空题(4分 * 15) 15 个 涉及的面很广的选择题,可是比較側重基础.包含数据结构的.c++类的,操作系统的,计算机网络的. 二:编程题(2 ...

  3. jsp中获取spring 管理的bean(通过config)

    WebApplicationContext wac = (WebApplicationContext)config.getServletContext().getAttribute(WebApplic ...

  4. ES7前端异步玩法:async/await理解 js原生API妙用(一)

    ES7前端异步玩法:async/await理解   在最新的ES7(ES2017)中提出的前端异步特性:async.await. 什么是async.await? async顾名思义是“异步”的意思,a ...

  5. 使用 kexec 快速重启 Linux

    http://www.ibm.com/developerworks/cn/linux/l-kexec/ 简介: 即使您的工作不要求您每天多次重新启动您的 Linux 机器,等待系统启动也实在是一件枯燥 ...

  6. Mac OS command line TestNG - “Cannot find class in classpath” error

    直接eclipse执行.xml文件可以正确执行 在mac下执行却总报错: [TestNG] [Error] Cannot find class in classpath 最后解决办法,classpat ...

  7. Drupal 初次使用感受,兴许补充。

    非常久曾经就接触过.下载下来安装,结果界面太丑,太难看,直接删除. 近期又一次想到开源CMS,好奇看到那么多人推崇drupal.也便下载来又一次研究了下. 刚接触了下.只是总体使用感觉非常差.尤其几个 ...

  8. LeetCode(125)题解--Valid Palindrome

    https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...

  9. 安装 jdk-8u121( jdk1.8 ) Eclipse jee neon java EE 4.6 并配置 中国科学技术大学 http://mirrors.ustc.edu.cn/eclipse/ 仓库源

    官网太慢用百度网盘,打不开刷新几次试试 http://pan.baidu.com/s/1qYTUrGK#list/path=%2F 首先下载安装 jdk-8u121-windows-x64.exe   ...

  10. EasyHLS直播在Linux非root用户运行时出现无法写文件的问题解决mkdir 0777

    今天在Github上收到一个用户反馈的EasyHLS在linux上非root用户调用时,无法写目录的问题:https://github.com/EasyDarwin/EasyHLS/issues/3, ...