题目1链接:SPOJ DISUBSTR - Distinct Substrings
题目1链接:SPOJ SUBST1 - New Distinct Substrings
DISUBSTR - Distinct Substrings
no tags
Given a string, we need to find the total number of its distinct substrings.
【SPOJ DISUBSTR - Distinct Substrings or SUBST1 - New 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
题意:给定一个字符串,求不同子串的数目。
思路:每一个子串一定是某些后缀的前缀,我们只需要统计所有后缀里面的不同前缀即可。对于后缀sa[i]而言,它的前缀有n-sa[i]个。而对于两个相邻的后缀sa[i-1],sa[i],重复的前缀有H[i]个,我们从前到后扫一遍即可。
第二道题把数组开大点就OK了。
AC代码:
//#include
#include
#include
#include
#include
#include
#include
#include
#include
推荐阅读