SPOJ(后缀数组求不同子串个数)
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.
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int MAXN=;
- char buf[MAXN];
- int sa[MAXN];
- int rnk[MAXN];
- int tmp[MAXN];
- int lcp[MAXN];
- int len,k;
- bool comp(int i,int j)
- {
- if(rnk[i]!=rnk[j]) return rnk[i]<rnk[j];
- else{
- int ri=(i+k<=len)?rnk[i+k]:-;
- int rj=(j+k<=len)?rnk[j+k]:-;
- return ri<rj;
- }
- }
- void getsa()
- {
- memset(rnk,,sizeof(rnk));
- memset(sa,,sizeof(sa));
- memset(tmp,,sizeof(tmp));
- len=strlen(buf);
- for(int i=;i<len;i++)
- {
- sa[i]=i;
- rnk[i]=buf[i]-'A';
- }
- sa[len]=len;
- rnk[len]=-;
- for(k=;k<=len;k*=)
- {
- sort(sa,sa+len+,comp);
- tmp[sa[]]=;
- for(int i=;i<=len;i++)
- {
- tmp[sa[i]]=tmp[sa[i-]]+(comp(sa[i-],sa[i])?:);
- }
- for(int i=;i<=len;i++)
- {
- rnk[i]=tmp[i];
- }
- }
- }
- void getlcp()
- {
- memset(rnk,,sizeof(rnk));
- memset(lcp,,sizeof(lcp));
- getsa();
- for(int i=;i<=len;i++)
- {
- rnk[sa[i]]=i;
- }
- int h=;
- lcp[]=h;
- for(int i=;i<len;i++)
- {
- int j=sa[rnk[i]-];
- if(h>) h--;
- for(;h+i<len&&h+j<len;h++)
- if(buf[i+h]!=buf[j+h]) break;
- lcp[rnk[i]-]=h;
- }
- }
- void debug()
- {
- for(int i=;i<=len;i++)
- {
- int l=sa[i];
- if(l==len)
- {
- printf("%d %d\n",l,lcp[i]);
- }
- else
- {
- for(int j=l;j<len;j++)
- printf("%c ",buf[j]);
- printf("%d\n",lcp[i]);
- }
- }
- }
- int T;
- int main()
- {
- scanf("%d",&T);
- while(T--)
- {
- scanf("%s",buf);
- int res=;
- getlcp();
- res+=(len+)*len/;
- for(int i=;i<=len;i++)
- res-=lcp[i];
- printf("%d\n",res);
- }
- return ;
- }
SPOJ(后缀数组求不同子串个数)的更多相关文章
- SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- [spoj DISUBSTR]后缀数组统计不同子串个数
题目链接:https://vjudge.net/contest/70655#problem/C 后缀数组的又一神奇应用.不同子串的个数,实际上就是所有后缀的不同前缀的个数. 考虑所有的后缀按照rank ...
- SPOJ SUBST1 New Distinct Substrings(后缀数组 本质不同子串个数)题解
题意: 问给定串有多少本质不同的子串? 思路: 子串必是某一后缀的前缀,假如是某一后缀\(sa[k]\),那么会有\(n - sa[k] + 1\)个前缀,但是其中有\(height[k]\)个和上一 ...
- spoj 694. Distinct Substrings 后缀数组求不同子串的个数
题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...
- spoj705 后缀数组求不同子串的个数
http://www.spoj.com/problems/SUBST1/en/ 题目链接 SUBST1 - New Distinct Substrings no tags Given a stri ...
- poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14874 Accepted: 5118 De ...
- 【POJ2774】Long Long Message(后缀数组求Height数组)
点此看题面 大致题意: 求两个字符串中最长公共子串的长度. 关于后缀数组 关于\(Height\)数组的概念以及如何用后缀数组求\(Height\)数组详见这篇博客:后缀数组入门(二)--Height ...
- poj 1743 后缀数组 求最长不重叠重复子串
题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...
- SPOJ REPEATS Repeats (后缀数组 + RMQ:子串的最大循环节)题解
题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[ ...
随机推荐
- Android HandlerThread 消息循环机制之源代码解析
关于 HandlerThread 这个类.可能有些人眼睛一瞟,手指放在键盘上,然后就是一阵狂敲.立即就能敲出一段段华丽的代码: HandlerThread handlerThread = new Ha ...
- JD笔试试题(凭记忆写的+人生感悟 try finally )
京东笔试:技术篇(一套卷.包含測试.算法,研发) 一:填空题(4分 * 15) 15 个 涉及的面很广的选择题,可是比較側重基础.包含数据结构的.c++类的,操作系统的,计算机网络的. 二:编程题(2 ...
- jsp中获取spring 管理的bean(通过config)
WebApplicationContext wac = (WebApplicationContext)config.getServletContext().getAttribute(WebApplic ...
- ES7前端异步玩法:async/await理解 js原生API妙用(一)
ES7前端异步玩法:async/await理解 在最新的ES7(ES2017)中提出的前端异步特性:async.await. 什么是async.await? async顾名思义是“异步”的意思,a ...
- 使用 kexec 快速重启 Linux
http://www.ibm.com/developerworks/cn/linux/l-kexec/ 简介: 即使您的工作不要求您每天多次重新启动您的 Linux 机器,等待系统启动也实在是一件枯燥 ...
- Mac OS command line TestNG - “Cannot find class in classpath” error
直接eclipse执行.xml文件可以正确执行 在mac下执行却总报错: [TestNG] [Error] Cannot find class in classpath 最后解决办法,classpat ...
- Drupal 初次使用感受,兴许补充。
非常久曾经就接触过.下载下来安装,结果界面太丑,太难看,直接删除. 近期又一次想到开源CMS,好奇看到那么多人推崇drupal.也便下载来又一次研究了下. 刚接触了下.只是总体使用感觉非常差.尤其几个 ...
- LeetCode(125)题解--Valid Palindrome
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...
- 安装 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 ...
- EasyHLS直播在Linux非root用户运行时出现无法写文件的问题解决mkdir 0777
今天在Github上收到一个用户反馈的EasyHLS在linux上非root用户调用时,无法写目录的问题:https://github.com/EasyDarwin/EasyHLS/issues/3, ...