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;
}
}

705. New Distinct Substrings spoj(后缀数组求所有不同子串)的更多相关文章

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

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

  2. SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)

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

  3. SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  4. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

  5. 【SPOJ694&705】Distinct Substrings(后缀数组)

    题意:求一个字符串的不相同的子串个数 n<=1000 思路:这是一道论文题 ..]of longint; n,i,m,ans,v,cas:longint; ch:ansistring; proc ...

  6. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...

  7. New Distinct Substrings(后缀数组)

    New Distinct Substrings(后缀数组) 给定一个字符串,求不相同的子串的个数.\(n<=50005\). 显然,任何一个子串一定是后缀上的前缀.先(按套路)把后缀排好序,对于 ...

  8. SPOJ 705 Distinct Substrings(后缀数组)

    [题目链接] http://www.spoj.com/problems/SUBST1/ [题目大意] 给出一个串,求出不相同的子串的个数. [题解] 对原串做一遍后缀数组,按照后缀的名次进行遍历, 每 ...

  9. spoj - Distinct Substrings(后缀数组)

    Distinct Substrings 题意 求一个字符串有多少个不同的子串. 分析 又一次体现了后缀数组的强大. 因为对于任意子串,一定是这个字符串的某个后缀的前缀. 我们直接去遍历排好序后的后缀字 ...

随机推荐

  1. Python 的经典入门书籍有哪些?

    是不是很多人跟你说,学Python开发就该老老实实地找书来看,再配合死命敲代码?电脑有了,软件也有了,心也收回来了?万事俱备,唯独只欠书籍?没找到到合适的书籍?可以看看这些. 1.Python基础教程 ...

  2. bugly cocos 接入和 符号表使用

    bugly cocos 接入和 符号表使用 在bugly网站下载 BuglyCocosPlugin 的sdk ios 1. 在 项目的 classes 里面新建 文件夹  BuglyCocosPlug ...

  3. HTTP请求范例

    package com.grefr.basemethod; /*JAVA发送HTTP请求,返回HTTP响应内容,实例及应用 博客分类: JAVA实现 Java.netBeanJDKApache . J ...

  4. IBM与麻省理工学院联合建立AI实验室 承诺投资2.4亿美元

         IBM和麻省理工学院将通过今天宣布的一个新的联合实验室共同努力,对人工智能进行广泛的研究.麻省理工学院IBM沃森AI实验室将重点关注四个研究支柱:开发AI算法,使用物理学来创建AI计算的新硬 ...

  5. 第3阶段——内核启动分析之make uImage编译内核(3)

    目标: 通过分析makefile,明白make uImage如何编译内核 把整个内核的makefile分成三类(makefile资料文档在linux-2.6.22.6/Documentation/bu ...

  6. Apache开启压缩功能

    起源 在一般的web服务器中,都会开启压缩功能,也就是deflate或者是gzip的压缩. 开启压缩功能主要的目的是为了减少传输的带宽,从而当服务器响应给客户端的时候,会大大减少传输的数据,代价就是在 ...

  7. [[NSBundle mainBundle] pathForResource:fileName ofType:]获取文件路径不成功

    目标文件明明已经加入项目了,但是使用[[NSBundle mainBundle] pathForResource:fileName ofType:]来获取文件路径的时候却为nil: 遇到这个问题大家需 ...

  8. yum的初步了解与使用

    什么是yum Yum(Yellow dog Updater,Modified)是一个基于RPM包管理的字符前端软件包管理器.能够从指定的服务器自动下载RPM包并且安装,可解决软件包相关依赖性,并且一次 ...

  9. 手机设备访问PC页面如果跳转到手机页面?

    //例如:iphone访问www.baidu.com自动跳转到wap.baidu.com,只需在pc端模版页面引入以下js代码//pc zhuan mobile var mobileAgent = n ...

  10. 团队作业4——第一次项目冲刺(Alpha版本)2017.4.23

    1.当天站立式会议照片 本次会议为第一次会议 本次会议在5号公寓1楼召开,本次会议内容: ①:做第一天的简单分工 ②:讨论每个人是否对安排的任务有苦难 ③:规定完成时间是在第二天之前 ④:遇到困难,及 ...