题目链接:https://vjudge.net/contest/70655#problem/C

后缀数组的又一神奇应用。不同子串的个数,实际上就是所有后缀的不同前缀的个数。

考虑所有的后缀按照rank排好了,我们现在已知height,也就是相邻的两个的最长公共前缀是多少。那么不同的子串个数怎么统计呢?

从第一个串开始考虑,ans+=L1。再看第二个串,会加进来几个不同的前缀呢?就是ans+=L2-height[2]。第三个类似,会加进来ans+=L3-height[3]……

因此最后的结果就是ans=L*(L+1)/2-sigma(height[2..n])。L是整个字符串的长度。

不过看这个题的数据范围是可以hash搞过去的,但是这个题就不行了:https://vjudge.net/contest/70655#problem/D,而且这个题会爆long long......

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std; const int maxn=; #define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[maxn*],wb[maxn*],wv[maxn*],wss[maxn*];
int c0(int *r,int a,int b)
{
return r[a]==r[b] && r[a+]==r[b+] && r[a+]==r[b+];
}
int c12(int k,int *r,int a,int b)
{
if (k==) return r[a]<r[b] || (r[a]==r[b]&&c12(,r,a+,b+));
else return r[a]<r[b] || (r[a]==r[b]&&wv[a+]<wv[b+]);
}
void sort(int *r,int *a,int *b,int n,int m)
{
int i;
for (i=;i<n;i++) wv[i]=r[a[i]];
for (i=;i<m;i++) wss[i]=;
for (i=;i<n;i++) wss[wv[i]]++;
for (i=;i<m;i++) wss[i]+=wss[i-];
for (i=n-;i>=;i--) b[--wss[wv[i]]]=a[i];
}
void dc3(int *r,int *sa,int n,int m)
{
int i,j,*rn=r+n;
int *san=sa+n,ta=,tb=(n+)/,tbc=,p;
r[n]=r[n+]=;
for (i=;i<n;i++) if (i%!=) wa[tbc++]=i;
sort(r+,wa,wb,tbc,m);
sort(r+,wb,wa,tbc,m);
sort(r,wa,wb,tbc,m);
for (p=,rn[F(wb[])]=,i=;i<tbc;i++)
rn[F(wb[i])]=c0(r,wb[i-],wb[i])?p-:p++;
if (p<tbc) dc3(rn,san,tbc,p);
else for (i=;i<tbc;i++) san[rn[i]]=i;
for (i=;i<tbc;i++) if (san[i]<tb) wb[ta++]=san[i]*;
if (n%==) wb[ta++]=n-;
sort(r,wb,wa,ta,m);
for (i=;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
for (i=,j=,p=;i<ta&&j<tbc;p++)
sa[p]=c12(wb[j]%,r,wa[i],wb[j])?wa[i++]:wb[j++];
for (;i<ta;p++) sa[p]=wa[i++];
for (;j<tbc;p++) sa[p]=wb[j++];
}
void da(int str[],int sa[],int rank[],int height[],int n,int m)
{
for (int i=n;i<n*;i++)
str[i]=;
dc3(str,sa,n+,m);
int i,j,k=;
for (i=;i<=n;i++) rank[sa[i]]=i;
for (i=;i<n;i++)
{
if (k) k--;
j=sa[rank[i]-];
while (str[i+k]==str[j+k]) k++;
height[rank[i]]=k;
}
}
char s[maxn];
int a[maxn*];
int ra[maxn*],height[maxn*],sa[maxn*]; int solve(int n)
{
// height[2..n]
int ans=n*(n+)/;
for (int i=;i<=n;i++) ans-=height[i];
return ans;
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%s",s);
int l=strlen(s);
for (int i=;i<l;i++) a[i]=(int)s[i];
da(a,sa,ra,height,l,);
printf("%d\n",solve(l));
}
return ;
}

[spoj DISUBSTR]后缀数组统计不同子串个数的更多相关文章

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

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

  2. SPOJ SUBST1 New Distinct Substrings(后缀数组 本质不同子串个数)题解

    题意: 问给定串有多少本质不同的子串? 思路: 子串必是某一后缀的前缀,假如是某一后缀\(sa[k]\),那么会有\(n - sa[k] + 1\)个前缀,但是其中有\(height[k]\)个和上一 ...

  3. SPOJ DISUBSTR 后缀数组

    题目链接:http://www.spoj.com/problems/DISUBSTR/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字 ...

  4. SPOJ DISUBSTR ——后缀数组

    [题目分析] 后缀数组模板题. 由于height数组存在RMQ的性质. 那么对于一个后缀,与前面相同的串总共有h[i]+sa[i]个.然后求和即可. [代码](模板来自Claris,这个板子太漂亮了) ...

  5. Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)

    求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...

  6. SPOJ(后缀数组求不同子串个数)

    DISUBSTR - Distinct Substrings Given a string, we need to find the total number of its distinct subs ...

  7. Distinct Substrings SPOJ - DISUBSTR 后缀数组

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

  8. SPOJ - DISUBSTR 多少个不同的子串

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

  9. Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)

    Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...

随机推荐

  1. 对URI的理解

    在了解RESTful api的设计规范的时候,遇到了一个问题,就是uri和url有什关系,有什么区别,所以就在这里记录一下. URI(Uniform Resource Identifier),统一资源 ...

  2. Table被web编程弃用的原因

    Table要比其它html标记占更多的字节. (延迟下载时间,占用服务器更多的流量资源.)Tablle会阻挡浏览器渲染引擎的渲染顺序. (会延迟页面的生成速度,让用户等待更久的时间.)Table里显示 ...

  3. 插头DP(基于连通性状态压缩的动态规划问题)(让你从入门到绝望)

    今天,我,Monkey king 又为大家带来大(ju)佬(ruo)的算法啦!--插头DP 例题(菜OJ上的网址:http://caioj.cn/problem.php?id=1489): 那么,这道 ...

  4. 深入了解jQuery Mobile-3装载器

    介绍 当jQuery Mobile通过Ajax加载内容或用于自定义通知时,会显示一个小的加载叠加层. 标准loader $( document ).on( "click", &qu ...

  5. Python3 列表,元组,字典,字符串知识小结

    一.知识概要 1. 列表,元组,字典,字符串的创建方式 2. 列表,元组,字典,字符串的方法调用 3. 列表,元组,字典,字符串的常规用法 二.列表 # 列 表 # 列表基础 list_1 = ['a ...

  6. .Net 面试题 汇总(一)

    1.@page指令只能在_aspx___文件(填写扩展名)中使用,而@Control指令只能用在_ascx___文件(填写扩展名)中使用. 2.说明控件DataGrid,DataTable,DataV ...

  7. python基础之socket套接字基础part2

    基于UDP的socket 面向无连接的不可靠数据传输,可以没有服务器端,只不过没有服务器端,发送的数据会被直接丢弃,并不能到达服务器端 1 #客户端 2 import socket 3 ip_port ...

  8. MVC使用ajax取得JSon数据

    为了在view中获取模型中的数据,用ajax异步模式读取数据,再用json返回的view中. 1.controller中: [HttpPost] public ActionResult GetAjax ...

  9. SpringBoot学习:IDEA中快速搭建springboot项目

    项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)IDEA中创建maven web项目 创建好项目后设置项目的编译路径: (二)引入 ...

  10. Lua工具类

    1.打印table --一个用以打印table的函数 function print_r (t, name) print(pr(t,name)) end function pr (t, name, in ...