P10469 后缀数组(如何算子串哈希值) · znzryb
## 思路讲解
我们来看看哈希怎么做
参考题解 [https://www.luogu.com.cn/article/qpf5y3pt](https://www.luogu.com.cn/article/qpf5y3pt)
那么其实最难的就是子串哈希的计算
哈希的递推公式是
$$
hash[i]=s[i]+base*hash[i-1]
$$
由此可得
$$
hash[r]=(s[r]+s[r-1]*base+\cdots+s[l-1]*base^{r-l+1}+\cdots+s[0]*base^{r})
$$
$$
hash[l-1]=(s[l-1]+s[l-2]*base+\cdots+s[0]*base^{l-1})
$$
$$
hash[l:r]=(s[r]+s[r-1]*base+\cdots+s[l]*base^{r-l})
$$
可以推得下式(其实可以这么理解,把 $hash[l-1]$ 左移 $r-l+1$ 位)
$$
hash[l:r]=hash[r]-hash[l-1]*base^{r-l+1}
$$
至于height数组?直接用定义求,不搞那么多花里胡哨的
当然哈希还是慢了一点

**下面是快排倍增做法**
[](https://www.notion.so/17c4539447bd80f1a1ede57547d2c6b4)
后缀的问题我们已经解决了,height数组是什么?

就是排序好的后缀,当前与上一个相同的前缀最长为多少(如上图所示)
那怎么求那?
运用下面这个定理


## AC代码
AC [https://www.luogu.com.cn/record/198571599](https://www.luogu.com.cn/record/198571599)
```cpp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
const ll MAXN=static_cast<ll>(1e6)+10;
const ll /*mod=static_cast<ll>(1e17)+3,*/base=131;
ll N,T,hashe[MAXN],sa[MAXN],height[MAXN],powBase[MAXN];
string S;
inline ll get_hash(ll a,ll b){
// if(a<b)
// return -1;
// 就是b的哈希值-(a-1)的哈希值取模,对a==0的情况特判了
return hashe[b]-(a>0?hashe[a-1]:0)*powBase[b-a+1];
}
inline bool comp(ll a,ll b){
// 使用二分查找来枚举重叠了几位
ll l=0,r=min(N-a,N-b);
while (l<r) {
ll mid=l+r+1>>1;
if(get_hash(a,a+mid-1)==get_hash(b,b+mid-1)){
l=mid;
}else{
r=mid-1;
}
}
if(a+l<N && b+l<N && S[a+l]!=S[b+l]){
return S[a+l]<S[b+l];
}
// 返回长度小的
return N-a<N-b;
}
inline void get_height(){
for(int i=1;i<N;++i){
ll a=sa[i-1],b=sa[i];
// 使用二分查找来枚举重叠了几位
ll l=0,r=min(N-a,N-b);
while (l<r) {
ll mid=l+r+1>>1;
if(get_hash(a, a+mid-1)==get_hash(b, b+mid-1)){
l=mid;
}else{
r=mid-1;
}
}
height[i]=l;
}
}
inline void init(){
ll val=0;
for(int i=0;i<N;++i){
val=base*val+S[i];
hashe[i]=val;
sa[i]=i;
}
powBase[0]=1;
for(int i=1;i<=N+5;++i){
powBase[i]=powBase[i-1]*base;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>S;
N=S.size();
init();
sort(sa, sa+N, comp);
for(int i=0;i<N;++i)
cout<<sa[i]<<' ';
cout<<'\n';
get_height();
for(int i=0;i<N;++i)
cout<<height[i]<<' ';
cout<<'\n';
}
/*
ponoiiipoi
AC https://www.luogu.com.cn/record/198571599
*/
```
AC 倍增法
[https://www.luogu.com.cn/record/198379595](https://www.luogu.com.cn/record/198379595)
```cpp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
const ll MAXN=static_cast<ll>(3e5)+117;
char S[MAXN];
// rk为第i个后缀子串排第几?
ll rk[MAXN],sa[MAXN],tmp[MAXN],height[MAXN];
ll N;
int K;
bool comp_sa(ll a,ll b){
if(rk[a]!=rk[b]){
return rk[a]<rk[b];
}else{
int ra= a+K<=N ? rk[a+K]:-1;
int rb= b+K<=N ? rk[b+K]:-1;
return ra<rb;
}
}
void cal_sa(){ // 计算后缀数组
// tmp要访问到第N个数
for(int i=0;i<=N;++i){
rk[i]=i<N ? S[i]:-1;
sa[i]=i; // 还没有排序的后缀数组
}
for(K=1;K<=N;K=K*2){
sort(sa, sa+N, comp_sa);
// 排在sa最前面的rk肯定也是最前面的
tmp[sa[0]]=0;
for(int i=0;i<N;++i){
// tmp[sa[0]]排第一个,tmp[sa[0]]为第
// 如果确实小,rk+1(因为这个rk要排logn次,不是一次排出来的)
tmp[sa[i+1]]=tmp[sa[i]]+(comp_sa(sa[i], sa[i+1]) ? 1:0);
}
for(int i=0;i<N;++i){
rk[i]=tmp[i];
}
}
}
void getHeight(){
for(int i=0;i<N;++i){
rk[sa[i]]=i;
}
for(int i=0,k=0;i<N;++i){
if(rk[i]==0) continue;
if(k) k--;
int j=sa[rk[i]-1]; // 前面一个序列的起始位置
while (i+k<N && j+k<N && S[i+k]==S[j+k]) k++;
height[rk[i]]=k;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>S;
N=strlen(S);
cal_sa();
for(int i=0;i<N;++i){
cout<<sa[i]<<' ';
}
cout<<'\n';
getHeight();
for(int i=0;i<N;++i){
cout<<height[i]<<' ';
}
cout<<'\n';
return 0;
}
/*
00000
5 4 3 2 1
AC https://www.luogu.com.cn/record/198379595
*/
```
## 心路历程(WA,TLE,MLE……)
WA
[https://www.luogu.com.cn/record/198357917](https://www.luogu.com.cn/record/198357917)
**字符串哈希WA了一次,主要原因是整型溢出**
[https://www.luogu.com.cn/record/198561313](https://www.luogu.com.cn/record/198561313)
解决方法竟然是不管?用整形溢出自然取整?我不理解,但我大受震撼