后缀数组
首先计算出sa, rank, height数组,
易知,不妨设x, y(rank[x] < rank[y]),那么x与y的最长公共前缀的长度便为min{height[i]}, rank[x] < i <= rank[y]
利用这个这个性质,便可以对枚举做出很大优化:
假设当前位置为p,则可以从rank[p]开始分别向两边进行枚举,当枚举到 minHeight <当前k 便退出
虽然我也不懂为什么快,但最近做了好几道,发现这种方法就是快(若有人知道,希望在评论区告知^_^)
以下代码可以在1s内跑过
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//后缀数组模板
/*
样例使用:
若原字符串为"abc"
则r = {1, 2, 3, 0};
n = 3;
GetSa(r, sa, n+1, m);
GetHeight(r, rank, height, n);
其中
r为字符串转化成的数组,末尾再添一个0
n为字符串的长度
sa[i]表示排名在i的后缀下标,i从1开始,满足Suffix(sa[i]) < Suffix(sa[i+1])
rank[i]表示Suffix(i)的排名,为sa的逆
height[i]表示sa[i]和sa[i-1]的最长公共前缀
*/
struct SuffixArray
{
enum {MAXN = 100010};
int wa[MAXN], wb[MAXN], wsf[MAXN], wv[MAXN];
int Cmp(int *r, int a, int b, int k)
{
return r[a] == r[b] && r[a+k] == r[b+k];
}
void GetSa(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
for (i = 0; i < m; i++)
wsf[i] = 0;
for (i = 0; i < n; i++)
wsf[x[i]=r[i]]++;
for (i = 1; i < m; i++)
wsf[i] += wsf[i-1];
for (i = n - 1; i >= 0; i--)
sa[--wsf[x[i]]] = i;
p = j = 1;
for (; 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++)
wsf[i] = 0;
for (i = 0; i < n; i++)
wsf[wv[i]]++;
for (i = 1; i < m; i++)
wsf[i] += wsf[i-1];
for (i = n - 1; i >= 0; i--)
sa[--wsf[wv[i]]] = y[i];
swap(x, y);
x[sa[0]] = 0;
for (p = 1, i = 1; i < n; i++)
x[sa[i]] = Cmp(y, sa[i-1], sa[i], j) ? p - 1 : p++;
}
}
void GetHeight(int *r, int *sa, int *rank, int *height, int n)
{
int i, j, k = 0;
for (i = 1; i <= n; i++)
rank[sa[i]] = i;
for (i = 0; i < n; i++)
{
if (k) k--;
else k=0;
j = sa[rank[i]-1];
while (r[i+k] == r[j+k]) k++;
height[rank[i]] = k;
}
}
}suffixArray;
const int MAXN = 100010;
const int STSIZE = 18;
char s[MAXN];
int r[MAXN], sa[MAXN], rnk[MAXN], height[MAXN], pre[MAXN], nxt[MAXN];
int main()
{
int T; scanf("%d", &T);
for (int kk = 1; kk <= T; kk++)
{
//input
scanf("%s", s);
//initialize
int n = 0;
for (int i = 0; s[i] != '\0'; i++)
r[n++] = s[i] - 'a' + 1;
r[n] = 0;
//get sa & rank & height
suffixArray.GetSa(r, sa, n + 1, 27);
suffixArray.GetHeight(r, sa, rnk, height, n);
//get pre & nxt
for (int i = 1; i <= n; i++)
{
if (height[i] == 0) pre[i] = i;
else pre[i] = pre[i-1];
}
for (int i = n; i > 0; i--)
{
if (i == n || height[i+1] == 0) nxt[i] = i;
else nxt[i] = nxt[i+1];
}
//get answer
printf("Case #%d:\n", kk);
int i = 0;
while (i < n)
{
int now = rnk[i];
int left = pre[now], right = nxt[now];
int t = i, k = 0;
int minHeight = height[now];
for (int j = now - 1; j >= left; j--)
{
minHeight = min(minHeight, height[j+1]);
if (minHeight < k) break;
if (sa[j] < i)
{
if ((minHeight == k && sa[j] < t) || minHeight > k)
{
t = sa[j];
k = minHeight;
}
}
}
if (now + 1 <= right) minHeight = height[now+1];
for (int j = now + 1; j <= right; j++)
{
minHeight = min(minHeight, height[j]);
if (minHeight < k) break;
if (sa[j] < i)
{
if ((minHeight == k && sa[j] < t) || minHeight > k)
t = sa[j];
k = minHeight;
}
}
if (k == 0) printf("-1 %d\n", s[i]);
else printf("%d %d\n", k, t);
i += (k == 0 ? 1 : k);
}
}
return 0;
}