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 就是让你求一下一个串的本质不同的子串的个数。
这个就等价于求一下后缀自动机每个节点的权值和,每个节点的权值等于(max{}-min{}+1)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
#define maxn 200005
int f[maxn],ch[maxn][26],cnt=1;
int n,siz[maxn],l[maxn],T,las=1;
int a[maxn],c[maxn];
char s[maxn];
ll ans=0; inline void init(){
cnt=las=1;
memset(f,0,sizeof(f));
memset(ch,0,sizeof(ch));
memset(c,0,sizeof(c));
siz[1]=l[1]=ans=0;
} inline void ins(int x){
int p=las,np=++cnt;
las=np,l[np]=l[p]+1;
siz[np]=1; for(;p&&!ch[p][x];p=f[p]) ch[p][x]=np;
if(!p) f[np]=1;
else{
int q=ch[p][x];
if(l[q]==l[p]+1) f[np]=q;
else{
int nq=++cnt;
l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
f[nq]=f[q];
f[q]=f[np]=nq;
for(;ch[p][x]==q;p=f[p]) ch[p][x]=nq;
}
}
} inline void build(){
for(int i=0;i<n;i++) ins(s[i]-'a');
} inline void solve(){
for(int i=1;i<=cnt;i++) ans+=(ll)(l[i]-l[f[i]]);
} int main(){
scanf("%d",&T);
while(T--){
init();
scanf("%s",s);
n=strlen(s);
build();
solve();
printf("%lld\n",ans);
} return 0;
}

  

 

最新文章

  1. Visual Studio (VS IDE) 你必须知道的功能和技巧 - 【.Net必知系列】
  2. C#窗体 LISTVIEW
  3. P1391 走廊泼水节
  4. C++ struct 定义及初始化
  5. 20145120黄玄曦 《java程序设计》 寒假学习总结
  6. 问题-安装XP时,提示不识别SATA硬盘
  7. ACPI电源管理中的S0 S1 S2 S3 S4 S5
  8. jquery ajax局部加载方法介绍
  9. 关于Java中Eclipse运行卡顿、未响应,Cpu100%的快速处理办法
  10. 翻译:CREATE DATABASE语句
  11. Ionic2 cordova angular2 打包到Android apk环境搭建
  12. HDU1348 Wall 凸包
  13. springboot2.x里面访问静态资源的坑
  14. tcc-transaction 分析
  15. 【20190228】JavaScript-数组的操作
  16. VScode编辑器使用
  17. 还原MongoDB dump备份出来的Bson数据
  18. 【转】每天一个linux命令(42):kill命令
  19. 【Python】模块
  20. MyBatis 返回(批量)新增数据的自增id

热门文章

  1. JMeter学习笔记(六) 文件下载接口测试
  2. python3 虚拟环境配置
  3. wxPython 界面编程的有关事件
  4. C#向上转型与向下转型(转)
  5. 孤荷凌寒自学python第二十三天python类的封装
  6. HDU 4763 Theme Section ( KMP next函数应用 )
  7. spring 配置问题记录1-@ResponseBody和favorPathExtension
  8. [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]
  9. CORS跨域cookie传递
  10. 【HDU 5961 传递】