SPOJ694/DISUBSTR:Distinct Substrings——题解
https://vjudge.net/problem/SPOJ-DISUBSTR
https://www.luogu.org/problemnew/show/SP694
http://www.spoj.com/problems/DISUBSTR/en/
给定一个字符串,求不相同的子串的个数。
参考罗穗骞论文。
显然一个子串可以定义为一个后缀的前缀。
我们还可以求出来相邻排名的后缀之间的最长公共前缀的长度。
答案就是所有子串数量-所有height。
(其实大胆猜想,不用证明即可(滑稽))
说下原理。
首先height数组含义可以转换成两个后缀的公共前缀个数。
原问题等价于求所有后缀之间不同的前缀个数。
那么按照排名加入的新的后缀suffix(sa[k])时,它会产生(n-sa[k]+1)个新的前缀,但是有height[k]个前缀是重复的,且和前面加入的所有后缀之间重复的前缀最多也不超过height[k]个(显然)。
所以只有height[k]个重复,我们把它扣掉即可。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int N=;
char s[N];
int n,rk[N],sa[N],height[N],w[N];
inline bool pan(int *x,int i,int j,int k){
int ti=i+k<n?x[i+k]:-;
int tj=j+k<n?x[j+k]:-;
return x[i]==x[j]&&ti==tj;
}
inline void SA_init(){
int *x=rk,*y=height,r=;
for(int i=;i<r;i++)w[i]=;
for(int i=;i<n;i++)w[s[i]]++;
for(int i=;i<r;i++)w[i]+=w[i-];
for(int i=n-;i>=;i--)sa[--w[s[i]]]=i;
r=;x[sa[]]=;
for(int i=;i<n;i++)
x[sa[i]]=s[sa[i]]==s[sa[i-]]?r-:r++;
for(int k=;r<n;k<<=){
int yn=;
for(int i=n-k;i<n;i++)y[yn++]=i;
for(int i=;i<n;i++)
if(sa[i]>=k)y[yn++]=sa[i]-k;
for(int i=;i<r;i++)w[i]=;
for(int i=;i<n;i++)++w[x[y[i]]];
for(int i=;i<r;i++)w[i]+=w[i-];
for(int i=n-;i>=;i--)sa[--w[x[y[i]]]]=y[i];
swap(x,y);r=;x[sa[]]=;
for(int i=;i<n;i++)
x[sa[i]]=pan(y,sa[i],sa[i-],k)?r-:r++;
}
for(int i=;i<n;i++)rk[i]=x[i];
}
inline void height_init(){
int i,j,k=;
for(i=;i<=n;i++)rk[sa[i]]=i;
for(i=;i<n;i++){
if(k)k--;
else k=;
j=sa[rk[i]-];
while(s[i+k]==s[j+k])k++;
height[rk[i]]=k;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
cin>>s;
n=strlen(s);
n++;
SA_init();
n--;
height_init();
int ans=(+n)*n/;;
for(int i=;i<=n;i++)ans-=height[i];
printf("%d\n",ans);
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
SPOJ694/DISUBSTR:Distinct Substrings——题解的更多相关文章
- spoj694 DISUBSTR - Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- SPOJ694 New Distinct Substrings
New Distinct Substrings 题目大意 给定一个字符串,求本质不同的子串个数 题解 SA常见思想:每一个子串都是某个后缀的前缀 考虑每一个后缀的贡献,首先他拥有n - sa[i]个( ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ DISUBSTR Distinct Substrings 后缀数组
题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...
- SPOJ 694 DISUBSTR - Distinct Substrings
思路 求本质不同的子串个数,总共重叠的子串个数就是height数组的和 总子串个数-height数组的和即可 代码 #include <cstdio> #include <algor ...
- SP694 DISUBSTR - Distinct Substrings
/* 统计每个节点的max和min, 然后求和即可 min = max[fa] + 1 */ #include<cstdio> #include<algorithm> #inc ...
- 【SPOJ694】Distinct Substrings (SA)
求不相同子串个数 该问题等价于求所有后缀间不相同前缀的个数..也就是对于每个后缀suffix(sa[i]),将贡献出n-sa[i]+1个,但同时,要减去那些重复的,即为height[i],故答案 ...
- SPOJ694 -- DISUBSTR 后缀树组求不相同的子串的个数
DISUBSTR - Distinct Substrings Given a string, we need to find the total number of its distinct su ...
随机推荐
- mybati缓存机制之二级缓存配置
二级缓存配置 在MyBatis的配置文件中开启二级缓存. <setting name="cacheEnabled" value="true"/> 在 ...
- lintcode702 连接两个字符串中的不同字符
连接两个字符串中的不同字符 给出两个字符串, 你需要修改第一个字符串,将所有与第二个字符串中相同的字符删除, 并且第二个字符串中不同的字符与第一个字符串的不同字符连接 思路:遍历两个字符串,找到互 ...
- [Clr via C#读书笔记]Cp12泛型
Cp12泛型 Generic: 特点 源代码保护 类型安全 清晰代码 更佳性能 Framework中的泛型 System.Collections.Generic; 开放类型,封闭类型:每个封闭类型都有 ...
- c++容器 STL
2019-01-24 22:30:32 记录学习PAT的一些知识,有待更新 注:本文是对Algorithm 算法笔记 的总结 C++标准库模板(Standard Template Library,ST ...
- NMAP-端口扫描
1.时序选项 -T0 -> -T5 速度变快,但是准确性下降,nmap默认是T3 2.指定端口 3.扫描指定TCP和UDP端口 4.快速扫描常见100个端口 5.扫描常见的n的端口 6.TCP ...
- Log Files
Description Nikolay has decided to become the best programmer in the world! Now he regularly takes p ...
- c# 生成的没用文件
1.pdb 2.vhost 3.application 4.含有更新功能(更新文件夹)
- UVALive - 6864 Strange Antennas 扫描线
题目链接: http://acm.hust.edu.cn/vjudge/problem/87213 Strange Antennas Time Limit: 3000MS 题意 一个雷达能够辐射到的范 ...
- sping框架(3)— 使用spring容器
spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.它们都可以代表spring容器,sp ...
- iOS-加载html字符串
NSMutableAttributedString * attrString =[[NSMutableAttributedString alloc] initWithData:[resultModel ...