/*
统计每个节点的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. ecstore-安装提示flock,即使绕过检测,安装成功后还是提示t function 解决办法

    安装时先绕过去 安装好后,修改config.php里的TMP_DIR,指向网站目录下的data目录(用绝对路径) 分析:应该是php的运行用户没有对tmp目录的写权限.中电云集的管理工具就出现过

  2. 代码编辑器之notepad++

    引用及下载地址:http://www.iplaysoft.com/notepad-plus.html NotePad++ 优秀的支持语法高亮的开源免费编辑器绿色版下载 EditPlus,它始终是一款收 ...

  3. github上传本地仓库

    前提是你已经有了自己的github账号,并且已经关联了本地和远程的仓库,设置好了ssh密钥.自行百度解决. 上传的主要命令: git init git add . git commit -m &quo ...

  4. 经常开发出现bug的同事,

    各位,再强调一点,以后如果遇到bug的地方,是自己开发的内容,我希望自己去看下调整下,而不是等再此被提出,等到领导再次提出问题,或者多次出现问题会影响自己的评级 现在是我再这里说,以后再其他地方工作, ...

  5. ALGO-147_蓝桥杯_算法训练_4-3水仙花数

    问题描述 打印所有100至999之间的水仙花数.所谓水仙花数是指满足其各位数字立方和为该数字本身的整数,例如 =^+^+^. 样例输入 一个满足题目要求的输入范例. 例: 无 样例输出 xxx xxx ...

  6. What is LBHttpSolrServer?

    LBHttpSolrServer or "Load Balanced HttpSolrServer" is just a wrapper to CommonsHttpSolrSer ...

  7. Java 内存溢出(java.lang.OutOfMemoryError)情况总结

    最近做一个项目,因为分了十几个模块,但是每次在Eclipse中启动Tomcat必须加载四五个模块,这样出现了 java.lang.OutOfMemoryError 原因是Eclipse中Tomcat设 ...

  8. 《Java并发编程实战》笔记-锁与原子变量性能比较

    如果线程本地的计算量较少,那么在锁和原子变量上的竞争将非常激烈.如果线程本地的计算量较多,那么在锁和原子变量上的竞争会降低,因为在线程中访问锁和原子变量的频率将降低. 在高度竞争的情况下,锁的性能将超 ...

  9. CSDN也有我的博客啦

    我的CSDN:https://blog.csdn.net/qq_40875849

  10. Oracle 官方文档地址

    官方文档地址: https://docs.oracle.com/cd/E11882_01/index.htm