/*
统计每个节点的max和min, 然后求和即可
min = max[fa] + 1 */
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#define ll long long
#define M 6010
#define mmp make_pair
using namespace std;
int read()
{
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int t, ch[M][26], len[M], fa[M], cnt, lst;
char s[M]; void insert(int c)
{
int p = ++cnt, f = lst;
len[p] = len[f] + 1;
lst = p;
while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
if(!f)
{
fa[p] = 1;
return;
}
int q = ch[f][c], nq = ++cnt;
if(len[f] + 1 == len[q])
{
fa[p] = q;
cnt--;
return;
}
len[nq] = len[f] + 1;
fa[nq] = fa[q];
fa[q] = fa[p] = nq;
memcpy(ch[nq], ch[q], sizeof(ch[nq]));
while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
} void init()
{
cnt = lst = 1;
memset(ch, 0, sizeof(ch));
memset(len, 0, sizeof(len));
memset(fa, 0, sizeof(fa));
} int main()
{
t = read();
while(t--)
{
init();
scanf("%s", s + 1);
int l = strlen(s + 1);
for(int i = 1; i <= l; i++) insert(s[i] - 'A');
int ans = 0;
for(int i = 1; i <= cnt; i++) ans += len[i] - len[fa[i]];
cout << ans << "\n";
}
return 0;
}

SP694 DISUBSTR - Distinct Substrings的更多相关文章

  1. DISUBSTR - Distinct Substrings

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

  2. spoj694 DISUBSTR - Distinct Substrings

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

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

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

  4. SPOJ DISUBSTR Distinct Substrings 后缀数组

    题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...

  5. SPOJ 694 DISUBSTR - Distinct Substrings

    思路 求本质不同的子串个数,总共重叠的子串个数就是height数组的和 总子串个数-height数组的和即可 代码 #include <cstdio> #include <algor ...

  6. SPOJ 694&&SPOJ705: Distinct Substrings

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

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

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

  8. SPOJ - Distinct Substrings,求不同的字串个数!

    DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...

  9. Distinct Substrings SPOJ - DISUBSTR 后缀数组

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

随机推荐

  1. bzoj 3600 没有人的算术——二叉查找树动态标号

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3600 已知 l 和 r 的排名,想快速知道 k 的排名.那么建一个 BIT ,用已知的排名做 ...

  2. 学习大数据基础框架hadoop需要什么基础

    什么是大数据?进入本世纪以来,尤其是2010年之后,随着互联网特别是移动互联网的发展,数据的增长呈爆炸趋势,已经很难估计全世界的电子设备中存储的数据到底有多少,描述数据系统的数据量的计量单位从MB(1 ...

  3. Spring Cloud(Dalston.SR5)--Eureka 常用配置

    配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...

  4. ML: 降维算法-PCA

            PCA (Principal Component Analysis) 主成份分析 也称为卡尔胡宁-勒夫变换(Karhunen-Loeve Transform),是一种用于探索高维数据结 ...

  5. [转]阿里云CentOS 7.1使用yum安装MySql5.6.24

    阿里云CentOS 7.1使用yum安装MySql5.6.24 作者:vfhky | 时间:2015-06-25 09:43 | 分类:web 在前面两篇文章<2015博客升级记(四):Cent ...

  6. Maven Gradle 区别

    Maven面临的挑战 软件行业新旧交替的速度之快往往令人咂舌,不用多少时间,你就会发现曾经大红大紫的技术已经成为了昨日黄花,当然,Maven也不会例外.虽然目前它基本上是Java构建的事实标准,但我们 ...

  7. 【java】之正则表达式摘要

    构造 匹配 字符 x 字符 x \\ 反斜线字符 \0n 带有八进制值 0 的字符 n (0 <= n <= 7) \0nn 带有八进制值 0 的字符 nn (0 <= n < ...

  8. android 退出程序解决内存释放so的问题

    做Android程序的时候发现一个问题,由于用到了so库,当应用程序退出了,但是手机变得很卡,点击"设置"查看应用程序,界面显示着可以点击"强制关闭". 由于这 ...

  9. 1049.(*) Counting Ones

    题意:题目大意:给出一个数字n,求1~n的所有数字里面出现1的个数 思路:转自(柳婼 の blog)遍历数字的低位到高位,设now为当前位的数字,left为now左边的所有数字构成的数字,right是 ...

  10. 【剑指offer】斐波那契数列非递归求解第N项

    public class Solution { public int Fibonacci(int n) { //错误输入处理 if(n<0) return -1; int pre = 1; in ...