Init
This commit is contained in:
153
ACMOJ-2031.cpp
Normal file
153
ACMOJ-2031.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
const int L_TYPE = 0;
|
||||
const int S_TYPE = 1;
|
||||
namespace __SA_IS {
|
||||
int a[2000100], sa[2000100], typ[2000100], c[1000100], p[2000100],
|
||||
sbuc[1000100], lbuc[1000100], name[1000100];
|
||||
|
||||
inline int islms(int *typ, int i) { return !typ[i] && (i == 1 || typ[i - 1]); }
|
||||
|
||||
int cmp(int *s, int *typ, int p, int q) {
|
||||
do {
|
||||
if (s[p] != s[q]) return 1;
|
||||
p++;
|
||||
q++;
|
||||
} while (!islms(typ, p) && !islms(typ, q));
|
||||
return (!islms(typ, p) || !islms(typ, q) || s[p] != s[q]);
|
||||
}
|
||||
|
||||
void isort(int *s, int *sa, int *typ, int *c, int n, int m) {
|
||||
int i;
|
||||
for (lbuc[0] = sbuc[0] = c[0], i = 1; i <= m; i++) {
|
||||
lbuc[i] = c[i - 1] + 1;
|
||||
sbuc[i] = c[i];
|
||||
}
|
||||
for (i = 1; i <= n; i++)
|
||||
if (sa[i] > 1 && typ[sa[i] - 1]) sa[lbuc[s[sa[i] - 1]]++] = sa[i] - 1;
|
||||
for (i = n; i >= 1; i--)
|
||||
if (sa[i] > 1 && !typ[sa[i] - 1]) sa[sbuc[s[sa[i] - 1]]--] = sa[i] - 1;
|
||||
}
|
||||
|
||||
void build_sa(int *s, int *sa, int *typ, int *c, int *p, int n, int m) {
|
||||
int i;
|
||||
for (i = 0; i <= m; i++) c[i] = 0;
|
||||
for (i = 1; i <= n; i++) c[s[i]]++;
|
||||
for (i = 1; i <= m; i++) c[i] += c[i - 1];
|
||||
typ[n] = 0;
|
||||
for (i = n - 1; i >= 1; i--)
|
||||
if (s[i] < s[i + 1])
|
||||
typ[i] = 0;
|
||||
else if (s[i] > s[i + 1])
|
||||
typ[i] = 1;
|
||||
else
|
||||
typ[i] = typ[i + 1];
|
||||
int cnt = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
if (!typ[i] && (i == 1 || typ[i - 1])) p[++cnt] = i;
|
||||
for (i = 1; i <= n; i++) sa[i] = 0;
|
||||
for (i = 0; i <= m; i++) sbuc[i] = c[i];
|
||||
for (i = 1; i <= cnt; i++) sa[sbuc[s[p[i]]]--] = p[i];
|
||||
isort(s, sa, typ, c, n, m);
|
||||
int last = 0, t = -1, x;
|
||||
for (i = 1; i <= n; i++) {
|
||||
x = sa[i];
|
||||
if (!typ[x] && (x == 1 || typ[x - 1])) {
|
||||
if (!last || cmp(s, typ, x, last))
|
||||
name[x] = ++t;
|
||||
else
|
||||
name[x] = t;
|
||||
last = x;
|
||||
}
|
||||
}
|
||||
for (i = 1; i <= cnt; i++) s[n + i] = name[p[i]];
|
||||
if (t < cnt - 1)
|
||||
build_sa(s + n, sa + n, typ + n, c + m + 1, p + n, cnt, t);
|
||||
else
|
||||
for (i = 1; i <= cnt; i++) sa[n + s[n + i] + 1] = i;
|
||||
for (i = 0; i <= m; i++) sbuc[i] = c[i];
|
||||
for (i = 1; i <= n; i++) sa[i] = 0;
|
||||
for (i = cnt; i >= 1; i--) sa[sbuc[s[p[sa[n + i]]]]--] = p[sa[n + i]];
|
||||
isort(s, sa, typ, c, n, m);
|
||||
}
|
||||
} // namespace __SA_IS
|
||||
void SA_IS(char *s, int n, int *sa, int *rk) {
|
||||
using namespace __SA_IS;
|
||||
for (int i = 1; i <= n; i++) a[i] = s[i];
|
||||
a[++n] = 0;
|
||||
build_sa(a, sa, typ, c, p, n, 256);
|
||||
n--;
|
||||
for (int i = 1; i <= n; i++) sa[i] = sa[i + 1];
|
||||
for (int i = 1; i <= n; i++) rk[sa[i]] = i;
|
||||
}
|
||||
void computeHeight(char *s, int n, int *sa, int *rk, int *height) {
|
||||
int k = 0;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (rk[i] == 1) {
|
||||
height[rk[i]] = 0;
|
||||
continue;
|
||||
}
|
||||
if (k > 0) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
LL GetPairMin(int *height, int n) {
|
||||
int *index_stack = 1 + new int[n + 10]();
|
||||
index_stack[-1] = 0;
|
||||
int s_cnt = 0;
|
||||
LL res = 0, cur = 0;
|
||||
index_stack[s_cnt++] = 1;
|
||||
for (int i = 2; i <= n; i++) {
|
||||
while (s_cnt > 0 && height[index_stack[s_cnt - 1]] >= height[i]) {
|
||||
s_cnt--;
|
||||
cur -= (index_stack[s_cnt] - index_stack[s_cnt - 1]) *
|
||||
(LL)height[index_stack[s_cnt]];
|
||||
}
|
||||
index_stack[s_cnt++] = i;
|
||||
cur += (index_stack[s_cnt - 1] - index_stack[s_cnt - 2]) * (LL)height[i];
|
||||
res += cur;
|
||||
}
|
||||
delete[] (index_stack - 1);
|
||||
return res;
|
||||
}
|
||||
LL calc(char *s, int n) {
|
||||
int *sa = new int[n << 4]();
|
||||
int *rk = new int[n << 4]();
|
||||
int *height = new int[n << 4]();
|
||||
SA_IS(s, n, sa, rk);
|
||||
computeHeight(s, n, sa, rk, height);
|
||||
LL res = GetPairMin(height, n);
|
||||
delete[] sa;
|
||||
delete[] rk;
|
||||
delete[] height;
|
||||
return res;
|
||||
}
|
||||
const int maxn = 2e5 + 10;
|
||||
char s1[maxn], s2[maxn];
|
||||
int main() {
|
||||
#ifdef local
|
||||
freopen("pro.in", "r", stdin);
|
||||
#endif
|
||||
scanf("%s%s", s1 + 1, s2 + 1);
|
||||
int n1 = strlen(s1 + 1);
|
||||
int n2 = strlen(s2 + 1);
|
||||
char *p = new char[n1 + n2 + 10]();
|
||||
for (int i = 1; i <= n1; i++) p[i] = s1[i];
|
||||
p[n1 + 1] = 'z' + 1;
|
||||
for (int i = 1; i <= n2; i++) p[n1 + 1 + i] = s2[i];
|
||||
LL ans = calc(p, n1 + 1 + n2);
|
||||
ans -= calc(s1, n1);
|
||||
ans -= calc(s2, n2);
|
||||
delete[] p;
|
||||
printf("%lld\n", ans);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user