705. New Distinct Substrings spoj(后缀数组求所有不同子串)
2024-08-26 00:27:15
705. New Distinct SubstringsProblem 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;
}
}
最新文章
- iOS三种正则表达式
- 阿里云配置nginx+php+mysql
- myeclipse高版本对应tomcat低版本解决办法
- nmap 高级扫描用法
- JavaScript脚本语言基础(一)
- Knockout.Js官网学习(click绑定)
- 使用Intellij IDEA从零使用Spring MVC
- 采用handle消息机制实现轮播效果
- spoj 375 Query on a tree(树链剖分,线段树)
- 使用CXF暴露您的REST服务
- ?Swift获取手机设备信息
- linux下安装apache2.4
- 输出图像到文件 imwrite()[OpenCV 笔记7]
- 免费下载获取Odoo中文实施 应用 指南 手册
- SkiaSharp图像处理
- Activiti6事件及监听器配置(学习笔记)
- Java集合框架面试题目
- 那一夜,风雪交加,我在搞jquery------专题之jquery选择器
- IL 学习笔记
- [UnityAPI]DataUtility类