后缀数组。按照排序完的后缀一个一个统计。每一个后缀对答案做出的贡献为:n-SA[i]-height[i]。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=+; int wa[maxn],wb[maxn],wv[maxn],WS[maxn];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=; i<m; i++) WS[i]=;
for(i=; i<n; i++) WS[x[i]=r[i]]++;
for(i=; i<m; i++) WS[i]+=WS[i-];
for(i=n-; i>=; i--) sa[--WS[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++) wv[i]=x[y[i]];
for(i=; i<m; i++) WS[i]=;
for(i=; i<n; i++) WS[wv[i]]++;
for(i=; i<m; i++) WS[i]+=WS[i-];
for(i=n-; i>=; i--) sa[--WS[wv[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++;
}
return;
} int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
return;
} int T,n,a[maxn],SA[maxn];
char str[maxn]; int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%s",str); n=strlen(str);
for(int i=;i<n;i++) a[i]=(int)str[i];
a[n]=; da(a,SA,n+,); calheight(a,SA,n);
LL ans=;
for(int i=;i<=n;i++) ans=ans+n-SA[i]-height[i];
printf("%lld\n",ans);
}
return ;
}

SPOJ 705 New Distinct Substrings的更多相关文章

  1. 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 ...

  2. 705. New Distinct Substrings spoj(后缀数组求所有不同子串)

    705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...

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

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

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

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

  5. SPOJ 694&&SPOJ705: Distinct Substrings

    DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...

  6. Spoj SUBST1 New Distinct Substrings

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

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

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

  8. 【SPOJ】694. Distinct Substrings

    http://www.spoj.com/problems/DISUBSTR/ 题意:求字符串不同子串的数目. #include <bits/stdc++.h> using namespac ...

  9. 【SPOJ 694】Distinct Substrings 不相同的子串的个数

    不会FQ啊,没法评测啊,先存一下代码QAQ 2016-06-16神犇Menci帮我测过AC了,谢谢神犇Menci QwQ #include<cstdio> #include<cstr ...

随机推荐

  1. lumen框架

    1,获取配置 app(); 说明:app()返回的是全局唯一的application对象,该对象的原型是Laravel\Lumen\Application 加载配置文件(config/app.php) ...

  2. IE8下 Select文字垂直居中的办法

    .select { padding: 4px 0; height: 30px; line-height: 26px; vertical-align: middle;} 处理前: 处理后:

  3. installshield学习笔记

    看预定义常量的值:SprintfBox(INFORMATION,"","%d",变量值);     自定义常量:#define MAXCOUNT 1000;#d ...

  4. 从0.5开始学Java 零

    作为新世纪的码农,掌握多门语言是必须的. 我先从java学起,工作又是做c# ,现在辞职了想重新捡回java的技术,所以写一个长篇 来给自己复习 ,题目就叫做从0.5开始学java. 核心内容就是 一 ...

  5. OpenWRT编译记录--TPLINK_WR841ND_V7

    之前自己编译OpenWRT的一些记录,现在搬上来.简单介绍了编译环境的准备,编译过程,以及一些注意事项. 准备工作 本人是在Ubuntu环境下编辑的,首先安装编译所需要的组件包: sudo apt-g ...

  6. struts2.3 创建工程

    1:在该网站下载struts2.3.16.3,目前为最新版.http://www.struts.apache.org/download.cgi 不妨下载“Full Distribution”版本 下载 ...

  7. 显示ubuntu 10.4右上角网络图标

    在面板右击“添加到面板”,选择“通知区域”

  8. hadoop 常用配置项【转】

    hadoop 常用配置项[转] core-site.xml  name value  Description   fs.default.name hdfs://hadoopmaster:9000 定义 ...

  9. 安装Redis无错流程

    1.参考文章<安装3.0.3版本配置文章参考>http://www.iyunv.com/thread-89612-1-1.html 2.安装tcl组件包(安装Redis需要tcl支持) 下 ...

  10. 如何在Sublime Text中添加代码片段

    我们在编写代码的时候,总会遇到一些需要反复使用的代码片段.这时候就需要反复的复制和黏贴,大大影响效率.我们利用Sublime Text的snippet(代码片段)功能,就能很好的解决这一问题.通俗的讲 ...