#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define inf 0x3f3f3f3f
#define ms(x) memset(x, 0, sizeof(x))
#define ll long long
using namespace std;
const int N = 200100;
int t1[N], t2[N], c[N];
int str[N], sa[N];
int ran[N], height[N];
bool cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a+l] == r[b+l];
}
void da(int str[], int n, int m)
{
int i, j, p, *x = t1, *y = t2;
for(i=0; i<m; i++) c[i] = 0;
for(i=0; i<n; i++) c[x[i] = str[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; j<=n; j<<=1)
{
p = 0;
for(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<m; i++) c[i] = 0;
for(i = 0; i<n; i++) c[x[y[i]]]++;
for(i = 1; i<m; i++) c[i]+=c[i-1];
for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1;
x[sa[0]] = 0;
for(i = 1; i<n; i++)
x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1:p++;
if(p>=n) break;
m = p;
}
int k = 0;
n--;
for(i=0; i<=n; i++) ran[sa[i]] = i;
for(i=0; i<n; i++)
{
if(k) k--;
j = sa[ran[i]-1];
while(str[i+k] == str[j+k]) k++;
height[ran[i]] = k;
}
}
int best[20][N];
int RMQ[N], mm[N];
void initRMQ(int n)
{
mm[0] = -1;
for(int i=1; i<=n; i++)
mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
for(int i=1; i<=n; i++) best[0][i] = i;
for(int i=1; i<=mm[n]; i++)
for(int j=1; j+(1<<i)-1<=n; j++)
{
int a = best[i-1][j];
int b = best[i-1][j+(1<<(i-1))];
if(height[a] < height[b]) best[i][j] = a;
else best[i][j] = b;
}
}
int askRMQ(int a, int b)
{
int t;
t = mm[b-a+1];
b-=(1<<t)-1;
a = best[t][a];
b=best[t][b];
return height[a] < height[b]?a:b;
}
int lcp(int a, int b)
{
a = ran[a], b = ran[b];
if(a>b) swap(a, b);
return height[askRMQ(a+1, b)];
}
后缀数组板子
最新推荐文章于 2019-10-21 22:24:02 发布