后缀数组。按照排序完的后缀一个一个统计。每一个后缀对答案做出的贡献为: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. dll间接应用问题

    在项目prj引用一个dll,a.dll,时,此dll应用b.dll 此时不将b.dll引用添加到prj,会有问题

  2. linux 通过pid寻找程序路径的最简单命令

    在linux实际操作命令中,查看pid的方式有很多种,通过pid找程序路径的方式也有好几个,但是可能大家都忽略的一个很简单也是很实用的命令:pwdx. 比如要查找某个java编写的程序运行情况可通过j ...

  3. My97DatePicker -- 一个功能丰富, 而且兼容 ie 6, 7的日期选择组件

    easyUI 也提供了 功能强大的日期组件, 可惜在ie 6,7,8下会报错,没有找到 addEventListener , JSON,  可能现在不想再支持低版本ie了 另外avalon也提供了 日 ...

  4. POJ2796/DP/单调栈

    题目链接[http://poj.org/problem?id=2796] 题意:给出一个数列,要求在这个数列里找到一个区间,使得在这个区间里的最小值*SUM[l,r]最大. 题解:思路来源于[http ...

  5. HDU2639[背包第K大]

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=2639] 题意:求第k大背包. 题解:利用二路归并的思想,求解第K大的值. #include<bi ...

  6. Python 第二课笔记

    1.模块初识 Python标准库和第三方库, 写好的功能封装好,起个名字,就是模块也叫库,直接导入就能用了. 标准库就是不需要安装,就能够导入的库 第三方库必须下载,安装才能使用的库. 1.1 两个标 ...

  7. 如何获取本机IP

    GetLocalHost 直接通过InetAddress.getLocalHost()来获取,其主要逻辑如下 InetAddress.getLocalHost(): String hostname = ...

  8. 给Linux添加google搜索命令

    一次面试时,面试官问怎么在终端直接做到在百度搜索自己的名字,当时没回答出来,面试官给了提示,问http协议.答案是说telnet连接www.baidu.com之后GET 昨天偶然看到一篇博客,http ...

  9. flume-hdfs sinks报错

    flume使用hdfs sinks时候报错:org.apache.flume.EventDeliveryException: java.lang.NullPointerException: Expec ...

  10. iOS开发QQ空间半透明效果的实现

    //1.首先我们可以确定的是cell的半透明, /* white The grayscale value of the color object, specified as a value from ...