hihocoder1415 后缀数组三·重复旋律3

传送门:http://hihocoder.com/problemset/problem/1415

【题解】

考虑求出两串合在一起(中间加分隔符)后缀数组,就是要求任意在两个串中的$i, j$,$\min\{h_k\} (i \leq k \leq j)$的最大值。

考虑$i, j$一定是满足$|i - j| = 1$且合法的时候最优。

详情见:hihocoder“解题方法提示”

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + ;
const int mod = 1e9+; # define rank RANK int n, ans;
char ch[M];
int sa[M], rank[M], h[M], t[M];
int tsa[M], cntA[M], cntB[M], A[M], B[M]; inline void getsa() {
fill(cntA, cntA + n + , );
for (int i=; i<=n; ++i) cntA[ch[i]] ++;
for (int i=; i<=n; ++i) cntA[i] += cntA[i-];
for (int i=n; i; --i) sa[cntA[ch[i]] --] = i;
rank[sa[]] = ;
for (int i=; i<=n; ++i) {
rank[sa[i]] = rank[sa[i-]];
if(ch[sa[i-]] != ch[sa[i]]) ++rank[sa[i]];
}
for (int len=; rank[sa[n]] < n; len <<= ) {
fill(cntA, cntA + n + , );
fill(cntB, cntB + n + , );
for (int i=; i<=n; ++i) {
cntA[A[i] = rank[i]] ++;
cntB[B[i] = ((i+len<=n) ? rank[i+len] : )] ++;
}
for (int i=; i<=n; ++i) cntA[i] += cntA[i-], cntB[i] += cntB[i-];
for (int i=n; i; --i) tsa[cntB[B[i]] --] = i;
for (int i=n; i; --i) sa[cntA[A[tsa[i]]] --] = tsa[i];
rank[sa[]] = ;
for (int i=; i<=n; ++i) {
rank[sa[i]] = rank[sa[i-]];
if(A[sa[i]] != A[sa[i-]] || B[sa[i]] != B[sa[i-]]) ++rank[sa[i]];
}
}
} inline void getheight() {
for (int i=, j=; i<=n; ++i) {
if(j) --j;
while(ch[i+j] == ch[sa[rank[i]-] + j]) ++j;
h[rank[i]] = j;
}
} int main() {
scanf("%s", ch+);
n = strlen(ch+);
int c = n;
ch[n+] = '$';
scanf("%s", ch+n+);
n = strlen(ch+);
// printf("%s\n", ch+1);
getsa(); getheight();
for (int i=; i<=n; ++i) {
int pre, suf;
pre = sa[i-], suf = sa[i];
if(pre > suf) swap(pre, suf);
if(pre <= c && suf > c) ans = max(ans, h[i]);
}
cout << ans;
return ;
}
上一篇:hihocoder #1415 : 后缀数组三·重复旋律3


下一篇:linux USR1亦通常被用来告知应用程序重载配置文件