#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x) x&-x;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int nMax = 2e5+5;
int s1[nMax];char s2[nMax];
int sa[nMax], rk[nMax], h[nMax];
int wa[nMax], wb[nMax], wv[nMax], c[nMax], vis[1005], id[nMax];
int ca;
int cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a+l] == r[b+l];
}
void get_SA(int *r, int n, int m){ // 倍增算法 r为待匹配数组 n为总长度 m为字符范围
int i, j, p, *x = wa, *y = wb, *t;
for(i = 0; i < m; i ++) c[i] = 0;
for(i = 0; i < n; i ++) c[x[i]=r[i]] ++;
for(i = 1; i < m; i ++) c[i] += c[i-1];
for(i = n-1; i >= 0; i --) sa[-- c[x[i]]] = i;
for(j = 1, p = 1; p < n; j *= 2, m = p){
for(p = 0, i = n-j; i < n; i ++) y[p ++] = i;
for(i = 0; i < n; i ++) if(sa[i] >= j) y[p ++] = sa[i] - j;
for(i = 0; i < n; i ++) wv[i] = x[y[i]];
for(i = 0; i < m; i ++) c[i] = 0;
for(i = 0; i < n; i ++) c[wv[i]] ++;
for(i = 1; i < m; i ++) c[i] += c[i-1];
for(i = n-1; i >= 0; i --) sa[-- c[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i ++){
x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p - 1: p ++;
}
}
}
void get_Height(int *r, int n){ // 求height数组。
int i, j, k = 0;
for(i = 1; i <= n; i ++) rk[sa[i]] = i;
for(i = 0; i < n; h[rk[i ++]] = k){
for(k ? k -- : 0, j = sa[rk[i]-1]; r[i+k] == r[j+k]; k ++);
}
}
后缀数组模板
最新推荐文章于 2021-08-14 21:36:30 发布