题解:

后缀数组求出height

然后ans=所有串-所有height

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=;
int t,ans,n,m,wa[N],wb[N],wsf[N],wv[N],sa[N],rank[N],height[N],s[N],a[N];
char str[N],str1[N],str2[N];
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
}
void getsa(int *r,int *sa,int n,int m)
{
int *x=wa,*y=wb;
for (int i=;i<m;i++)wsf[i]=;
for (int i=;i<n;i++)wsf[x[i]=r[i]]++;
for (int i=;i<m;i++)wsf[i]+=wsf[i-];
for (int i=n-;i>=;i--)sa[--wsf[x[i]]]=i;
for (int p=,j=;p<n;j*=,m=p)
{
p=;
for (int i=n-j;i<n;i++)y[p++]=i;
for (int i=;i<n;i++)
if (sa[i]>=j)y[p++]=sa[i]-j;
for (int i=;i<n;i++)wv[i]=x[y[i]];
for (int i=;i<m;i++)wsf[i]=;
for (int i=;i<n;i++)wsf[wv[i]]++;
for (int i=;i<m;i++)wsf[i]+=wsf[i-];
for (int i=n-;i>=;i--)sa[--wsf[wv[i]]]=y[i];
swap(x,y);x[sa[]]=;p=;
for (int i=;i<n;i++)x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
void getheight(int *r,int n)
{
int j,k=;
for (int i=;i<=n;i++)rank[sa[i]]=i;
for (int i=;i<n;i++)
{
if (k)k--;
else k=;
j=sa[rank[i]-];
while (r[i+k]==r[j+k])k++;
height[rank[i]]=k;
}
}
int main()
{
scanf("%d",&t);
while (t--)
{
scanf("%s",str);
int len=strlen(str);
for (int i=;i<len;i++)s[i]=str[i];
s[len]=;
getsa(s,sa,len+,);
getheight(s,len);
int ans=(+len)*len/;
for (int i=;i<=len;i++)ans-=height[i];
printf("%d\n",ans);
}
}

spoj705的更多相关文章

  1. Cogs 1709. [SPOJ705]不同的子串 后缀数组

    题目:http://cojs.tk/cogs/problem/problem.php?pid=1709 1709. [SPOJ705]不同的子串 ★★   输入文件:subst1.in   输出文件: ...

  2. 【spoj705】 Distinct Substrings

    [题目描述] 给定一个字符串,计算其不同的子串个数. [输入格式] 一行一个仅包含大写字母的字符串,长度<=50000 [输出格式] 一行一个正整数,即不同的子串个数. [样例输入] ABABA ...

  3. spoj705 后缀数组求不同子串的个数

    http://www.spoj.com/problems/SUBST1/en/  题目链接 SUBST1 - New Distinct Substrings no tags  Given a stri ...

  4. [SPOJ705]不同的子串

    题目描述] 给定一个字符串,计算其不同的子串个数. [输入格式] 一行一个仅包含大写字母的字符串,长度<=50000 [输出格式] 一行一个正整数,即不同的子串个数. [样例输入] ABABA ...

  5. SPOJ 694&&SPOJ705: Distinct Substrings

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

  6. SPOJ705 SUBST1 - New Distinct Substrings(后缀数组)

    给一个字符串求有多少个不相同子串. 每一个子串一定都是某一个后缀的前缀.由此可以推断出总共有(1+n)*n/2个子串,那么下面的任务就是找这些子串中重复的子串. 在后缀数组中后缀都是排完序的,从sa[ ...

  7. SPOJ705 Distinct Substrings (后缀自动机&后缀数组)

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

  8. [spoj694&spoj705]New Distinct Substrings(后缀数组)

    题意:求字符串中不同子串的个数. 解题关键:每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数. 1.总数减去height数组的和即可. 注意这里height中为什么不需 ...

  9. 北京培训记day2

    后缀三姐妹 P.S.后缀大家族关系:后缀自动机fail指针=后缀树,后缀树前序遍历=后缀数组 一.后缀数组:orz罗穗骞集训队论文 给每个后缀按字典序排序 rank[]表示从i开始的后缀排名多少 sa ...

随机推荐

  1. Android 虹软免费人脸识别 SDK开发

    目前我们的应用内使用了 ArcFace 的人脸检测功能,其他的我们并不了解,所以这里就和大家分享一下我们的集成过程和一些使用心得集成ArcFace FD 的集成过程非常简单在 ArcFace FD 的 ...

  2. Visual Studio 2015+InstallShield 2015

    下载Installshield http://learn.flexerasoftware.com/content/IS-EVAL-InstallShield-Limited-Edition-Visua ...

  3. 子序列的按位或 Bitwise ORs of Subarrays

    2018-09-23 19:05:20 问题描述: 问题求解: 显然的是暴力的遍历所有的区间是不可取的,因为这样的时间复杂度为n^2级别的,对于规模在50000左右的输入会TLE. 然而,最后的解答也 ...

  4. ico 图标 生成 工具 网站

    http://www.faviconico.org/favicon favicon.ico在线制作,在线Favicon.ico制作转换工具

  5. 雷林鹏分享:C# 结构(Struct)

    C# 结构(Struct) 在 C# 中,结构是值类型数据结构.它使得一个单一变量可以存储各种数据类型的相关数据.struct 关键字用于创建结构. 结构是用来代表一个记录.假设您想跟踪图书馆中书的动 ...

  6. c++-pimer-plus-6th-chapter06

    Chapter Review 1 Both version give the same answers, but the if else version is more efficient. Cons ...

  7. 11月26日 用seed,预加载种子文件; Case 条件语句。网址的参数如何传递,; Query--自定义scopes

    在seed文件中输入一些预加载的种子job,注意属性和值都要有:  ❌错误,我输入contact_email的时候value值是空的,这样不能正确生成. 正确✅: for i in 1..10 do ...

  8. fedora21 中lamp的搭建(测试没有问题)

    LAMP Stands for Linux,Apache,MySQL and PHP. Most of the websites works with the above combination. T ...

  9. Oracl 12c安装

    Oracl安装部署 一.前置条件准备 修改hostname: hostname oracle 修改/etc/hosts:添加192.168.10.106 oracle 添加软件开发工具 搭建yum源 ...

  10. 02 爬虫数据解析之re,xpath,beautifulsoup

    一.正则匹配 简单用法演示: 字符: print(re.findall(".","abccc31223dn哈哈")) ### . 匹配除了换行符以外的任意字符, ...