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

Input

T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 50000

Output

For each test case output one number saying the number of distinct substrings.

Example

Input:

2

CCCCC

ABABA

Output:

5

9

Solution

后缀排序后

一个后缀 \(SA[i]\) 有 \(n-SA[i]+1\) 个子串,但后缀 \(SA[i]\) 与 \(SA[i-1]\) 有 \(height[i]\) 个字符相同,那么就有 \(height[i]\) 个子串一样,减去就是了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=50000+10;
char s[MAXN];
int T,n,m,SA[MAXN],rk[MAXN],cnt[MAXN],nxt[MAXN],height[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void GetSA()
{
n=strlen(s+1),m=300;
for(register int i=1;i<=n;++i)rk[i]=s[i];
for(register int i=1;i<=m;++i)cnt[i]=0;
for(register int i=1;i<=n;++i)cnt[rk[i]]++;
for(register int i=1;i<=m;++i)cnt[i]+=cnt[i-1];
for(register int i=n;i>=1;--i)SA[cnt[rk[i]]--]=i;
for(register int k=1,ps;k<=n;k<<=1)
{
ps=0;
for(register int i=n-k+1;i<=n;++i)nxt[++ps]=i;
for(register int i=1;i<=n;++i)
if(SA[i]>k)nxt[++ps]=SA[i]-k;
for(register int i=1;i<=m;++i)cnt[i]=0;
for(register int i=1;i<=n;++i)cnt[rk[i]]++;
for(register int i=1;i<=m;++i)cnt[i]+=cnt[i-1];
for(register int i=n;i>=1;--i)SA[cnt[rk[nxt[i]]]--]=nxt[i];
std::swap(nxt,rk);
rk[SA[1]]=1;ps=1;
for(register int i=2;i<=n;rk[SA[i]]=ps,++i)
if(nxt[SA[i]]!=nxt[SA[i-1]]||nxt[SA[i]+k]!=nxt[SA[i-1]+k])ps++;
if(ps>=n)break;
m=ps;
}
for(register int i=1,j,k=0;i<=n;height[rk[i++]]=k)
for(k=k?k-1:k,j=SA[rk[i]-1];s[i+k]==s[j+k];++k);
}
inline int solve()
{
int ans=0;
for(register int i=1;i<=n;++i)ans+=n-SA[i]+1-height[i];
return ans;
}
int main()
{
read(T);
while(T--)
{
scanf("%s",s+1);
GetSA();
write(solve(),'\n');
}
return 0;
}

【刷题】SPOJ 705 SUBST1 - New Distinct Substrings的更多相关文章

  1. SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数

    题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...

  2. 后缀数组:SPOJ SUBST1 - New Distinct Substrings

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

  3. Spoj SUBST1 New Distinct Substrings

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

  4. SP705 SUBST1 - New Distinct Substrings

    \(\color{#0066ff}{ 题目描述 }\) 给定一个字符串,求该字符串含有的本质不同的子串数量. \(\color{#0066ff}{输入格式}\) T- number of test c ...

  5. spoj SUBST1 - New Distinct Substrings【SAM||SA】

    SAM里的转台不会有重复串,所以答案就是每个right集合所代表的串个数的和 #include<iostream> #include<cstdio> #include<c ...

  6. SPOJ SUBST1 New Distinct Substrings(后缀数组 本质不同子串个数)题解

    题意: 问给定串有多少本质不同的子串? 思路: 子串必是某一后缀的前缀,假如是某一后缀\(sa[k]\),那么会有\(n - sa[k] + 1\)个前缀,但是其中有\(height[k]\)个和上一 ...

  7. SPOJ 694 (后缀数组) Distinct Substrings

    将所有后缀按照字典序排序后,每新加进来一个后缀,它将产生n - sa[i]个前缀.这里和小罗论文里边有点不太一样. height[i]为和字典序前一个的LCP,所以还要减去,最终累计n - sa[i] ...

  8. SPOJ705 SUBST1 - New Distinct Substrings(后缀数组)

    给一个字符串求有多少个不相同子串. 每一个子串一定都是某一个后缀的前缀.由此可以推断出总共有(1+n)*n/2个子串,那么下面的任务就是找这些子串中重复的子串. 在后缀数组中后缀都是排完序的,从sa[ ...

  9. SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)

    SUBST1 - New Distinct Substrings no tags  Given a string, we need to find the total number of its di ...

随机推荐

  1. JAVA面试中问及HIBERNATE与 MYBATIS的对比

    第一方面:开发速度的对比 就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉得要用好Mybatis还是首先要先理解好H ...

  2. 探索 Flask

    探索 Flask 探索 Flask 是一本关于使用 Flask 开发 Web 应用程序的最佳实践和模式的书籍.这本书是由 426 名赞助人 在 Kickstarter 上 于 2013 年 7 月资助 ...

  3. redis 学习笔记二

    redis启动: 直接 redis-server.exe 启动服务,是按照redis默认配置启动的,如果想按照自己的配置文件启动,要加上 redis-server.exe  redis.windows ...

  4. 强制删除无用old windows文件夹命令

    磁盘上有旧系统留下的目录比如old.windows.program files.users(中文目录是用户,删除命令里还是要用user才有效),因为目录的特殊设置,导致无法直接删除,需要修改属性和权限 ...

  5. JAVA日志框架概述

            日志用来记录应用的运行状态以及一些关键业务信息,其重要性不言而喻,通常我们借助于现有的日志框架完成日志输出.目前开源的日志框架很多,常见的有log4j.logback等,有时候我们还会 ...

  6. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第3章.Tomcat

    第3章--Tomcat Tomcat安装与运行 Tomcat:目前最常用的基于java的web应用服务器 本课程中所有的Java代码最终都需要部署到Tomcat中运行 Tomcat的配置文件是XML的 ...

  7. 微服务框架Dubbo与Springcloud的区别

    微服务框架Dubbo与Springcloud的区别 微服务主要的优势如下: 1.降低复杂度 将原来偶合在一起的复杂业务拆分为单个服务,规避了原本复杂度无止境的积累.每一个微服务专注于单一功能,并通过定 ...

  8. 【第六章】MySQL日志文件管理

    1.日志文件管理概述: 配置文件:/etc/my.cnf 作用:MySQL日志文件是用来记录MySQL数据库客户端连接情况.SQL语句的执行情况以及错误信息告示. 分类:MySQL日志文件分为4种:错 ...

  9. Ubuntu—查看进程并关闭进程

    环境:Ubuntu终端 命令:ps -aux 功能:查看进程信息 命令:kill 进程号(PID) 功能:杀死进程

  10. 在 Ubuntu 下安装 Deepin 的 QQ、微信、百度云和迅雷等软件

    在以前的文章 Ubuntu 常用软件推荐(QQ.微信.MATLAB等)及安装过程 中,我们用 Wine QQ 和 Electronic Wechat 来解决 Ubuntu 系统下使用 QQ 和微信的难 ...