705. New Distinct Substrings

Problem code: SUBST1

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

Added by: Hoang Hong Quan
Date: 2006-01-18
Time limit: 2s
Source limit: 50000B
Memory limit: 256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages: All except: NODEJS PERL 6
Resource: Base on a problem in ByteCode06
 
 
 
 
 
 
 
 
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define N 51000
char a[N];
int c[N],d[N],e[N],sa[N],height[N],n,b[N],m;
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da()
{
int i,j,p,*x=c,*y=d,*t;
for(i=;i<m;i++)b[i]=;
for(i=; i<n; i++)b[x[i]=a[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++)y[p++]=i;
for(i=; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=; i<n; i++)e[i]=x[y[i]];
for(i=;i<m;i++)b[i]=;
for(i=; i<n; i++)b[e[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[e[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
void callheight()
{
int i,j,k=;
for(i=; i<n; i++)b[sa[i]]=i;
n--;
for(i=; i<n; height[b[i++]]=k)
for(k?k--:,j=sa[b[i]-]; a[i+k]==a[j+k]; k++);
} int main()
{
int t,i;
cin>>t;
for(i=;i<t;i++)
{
scanf("%s",a);
n=strlen(a);
n++;
m=;
da();
callheight();
int sum=;
for(int j=;j<=n;j++)sum+=n-sa[j]-height[j];
cout<<sum<<endl;
}
}

最新文章

  1. iOS三种正则表达式
  2. 阿里云配置nginx+php+mysql
  3. myeclipse高版本对应tomcat低版本解决办法
  4. nmap 高级扫描用法
  5. JavaScript脚本语言基础(一)
  6. Knockout.Js官网学习(click绑定)
  7. 使用Intellij IDEA从零使用Spring MVC
  8. 采用handle消息机制实现轮播效果
  9. spoj 375 Query on a tree(树链剖分,线段树)
  10. 使用CXF暴露您的REST服务
  11. ?Swift获取手机设备信息
  12. linux下安装apache2.4
  13. 输出图像到文件 imwrite()[OpenCV 笔记7]
  14. 免费下载获取Odoo中文实施 应用 指南 手册
  15. SkiaSharp图像处理
  16. Activiti6事件及监听器配置(学习笔记)
  17. Java集合框架面试题目
  18. 那一夜,风雪交加,我在搞jquery------专题之jquery选择器
  19. IL 学习笔记
  20. [UnityAPI]DataUtility类

热门文章

  1. flume 搭建
  2. TFLearn构建神经网络
  3. myeclipse eclipse创建maven web项目时 index.jsp报错
  4. BZOJ 3379: [Usaco2004 Open]Turning in Homework 交作业
  5. String类的重要方法与字段
  6. Qt--自定义Model
  7. 再起航,我的学习笔记之JavaScript设计模式21(命令模式)
  8. shell脚本编程基础
  9. !JS实战之随机像素图
  10. java环境安装说明