SP694 DISUBSTR - Distinct Substrings
/*
统计每个节点的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的更多相关文章
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- spoj694 DISUBSTR - Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ DISUBSTR Distinct Substrings 后缀数组
题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...
- SPOJ 694 DISUBSTR - Distinct Substrings
思路 求本质不同的子串个数,总共重叠的子串个数就是height数组的和 总子串个数-height数组的和即可 代码 #include <cstdio> #include <algor ...
- SPOJ 694&&SPOJ705: Distinct Substrings
DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...
- SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- SPOJ - Distinct Substrings,求不同的字串个数!
DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...
- Distinct Substrings SPOJ - DISUBSTR 后缀数组
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- mongo之 ReadConcern 与 Read Preference
一.读取关注(readConcern) 官方文档 3.2版本以后支持读取关注 读取关注允许您控制从副本集和副本集分片读取的数据的实时性,一致性和隔离性. 通过有效使用写入关注和读取关注,可以适当调整一 ...
- linux 自总结常用命令(centos系统)
查看apache2的命令 httpd -V 其中HTTPD_ROOT和SERVER_CONFIG_FILE 就可以确定httpd.conf(Apache配置文件)的路径了 apache启动.停止.重 ...
- 解决Ecipse和搜狗输入法快捷键冲突问题
非常简单,关闭掉搜狗输入的所有快捷键!
- eclipse卡死在search for main types 20 files to index
run as application时,提示search for main types 20 files to index (*/*/*.jar)某个maven依赖jar出了问题,找不到main方法 ...
- Flume 高可用配置案例+load balance负载均衡+ 案例:日志的采集及汇总
高可用配置案例 (一).failover故障转移 在完成单点的Flume NG搭建后,下面我们搭建一个高可用的Flume NG集群,架构图如下所示: (1)节点分配 Flume的Agent和Colle ...
- windbg 如何再内核模式调试用户空间的程序
1:使用!process 0 0 获取用户空间的所有的进程的信息 !process 0 0 **** NT ACTIVE PROCESS DUMP **** PROCESS 80a02a60 ...
- kindeditor在线编辑器的使用心得
1. 如何声明引用? <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- CPS(Cyber-Physical Systems)白皮书-摘选
<中国制造2025>提出,“基于信息物理系统的智能装备.智能工厂等智能制造正在引领制造方式变革”,要围绕控制系统.工业软件.工业网络.工业云服务和工业大数据平台等,加强信息物理系统的研发与 ...
- 使用 Visual Studio 分析器找出应用程序瓶颈
VS的性能分析工具 性能分析工具的选择 打开一个“性能分析”的会话:Debug->Start Diagnotic Tools Without Debugging(或按Alt+F2),VS2013 ...
- NIO基本操作
NIO是Java 4里面提供的新的API,目的是用来解决传统IO的问题 NIO主要有三大核心部分:Channel(通道),Buffer(缓冲区), Selector(选择器) Channel(通道) ...