/*
统计每个节点的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. buntu下备份系统的方法

    今天不小心强行结束了一不知道用处的进程,结果造成进不了x界面,gdm启动不了,使用apt-get进行修复,结果几乎要把整个x界面有关的软件包删除,所以只好重装系统,为了防止下次出现类似的问题,所以把系 ...

  2. express 与 koa 区别

    express 与 koa 区别 区别项 express koa 中间件模型 Compress 模型 洋葱圈模型 对象个数 只有2个对象:Request 和 Response 有3个对象:Reques ...

  3. 运行Docker镜像

    1. 检查本机Docker镜像 docker images 2. 下载hello-world镜像 docker pull hello-world 3. 运行docker镜像hello-world do ...

  4. FFMPEG-Java 入门

    注意:FFMPEG-Java 和 Jffmpeg 不是一回事.FFMPEG-Java 是 Freedom for Media in Java(缩写为 FMJ)的一个子项目.        这个项目遵循 ...

  5. 【java】break,continue和return区别

    break:适用于switch和loop continue:只适用于loop 两者都可以通过给循环加标签来控制跳出,如下例所示 class BreakDemo { public static void ...

  6. ubuntu-docker入门到放弃(二)docker初探(基本用法及命令)

    一.使用公共镜像 docker有一个开源的镜像管理系统,上面有很多常见的images,如mysql,nginx,tomcat等,可以直接根据自己的需求下载下来用,还有系统images,如redhat, ...

  7. win7 上运行 php7 +

    win7 安装 php7+ 很简单, 这里不赘述 如何在phpstudy 添加   php7   百度也很容易找到. 但是在 php 7 运行的时候总是报0x0000007  或者 缺少 .dll 文 ...

  8. 黄聪:VPS服务器轻松备份工具配置

  9. 《亲测》nginx webscoket ssl conf配置示例

    非crt证书,用的pem,其中 http://localhost:5003 是你要转发到的站点网址 配置的就是 server { listen 80; server_name smarthome.yi ...

  10. springboot springcloud 热部署

    本文转载自:https://www.cnblogs.com/moly/p/7978303.html 可以在配置修改某些文件重启,建议配置一个控制文件,只有此文件修改才会重启. 1. pom中添加: & ...