forked from yogykwan/acm-challenge-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoj2292.cpp
199 lines (177 loc) · 4.08 KB
/
aoj2292.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* AOJ 2292: Common Palindromes
* 题意:给出两个字符串,从中各自取出长度相等的回文子串,起点终点的四元组构成一组合法解。求合法解个数。
* 类型:后缀数组+回文DP+并查集
* 算法:用Manacher求回文半径,倍增求后缀数组,尺取法求高度数组。按字符串奇偶分类,分别记录原串和复制串中符合长度的回文数量。按半径和lcp长度从大到小,枚举回文半径时更新对应中心的计数,将SA中相邻后缀合并时更新答案。
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <iostream>
using namespace std;
typedef long long LL;
char s[100010];
int n, k, ns, nt;
int m;
int sa[100010];
int ra[100010], tr[100010];
int lcp[100010];
char t[200010];
int rad[200010];
vector<int> q[100010], in[100010];
int pa[100010], ru[100010], a[100010], b[100010];
LL ret, ans;
bool CmpSa(int a, int b) {
if (ra[a] != ra[b]) return ra[a] < ra[b];
int aa = a + k < n ? ra[a + k] : -1;
int bb = b + k < n ? ra[b + k] : -1;
return aa < bb;
}
void GaoSa() {
for (int i = 0; i <= n; ++i) {
sa[i] = i;
ra[i] = i < n ? s[i] : -1;
}
for (k = 1; k <= n; k <<= 1) {
sort(sa, sa + n + 1, CmpSa);
tr[sa[0]] = 0;
for (int i = 1; i <= n; ++i) {
tr[sa[i]] = tr[sa[i - 1]] + CmpSa(sa[i - 1], sa[i]);
}
memcpy(ra, tr, sizeof(int) * (n + 1));
}
}
void GaoLcp() {
int tmp = 0;
for (int i = 0; i < n; ++i) {
int rk = ra[i];
int j = sa[rk - 1];
if (tmp > 0) --tmp;
while (i + tmp < n && j + tmp < n && s[i + tmp] == s[j + tmp]) {
++tmp;
}
lcp[rk - 1] = tmp;
}
}
void Manacher() {
for (int i = 0; i < n; ++i) {
t[i << 1] = '#';
t[i << 1 | 1] = s[i];
}
t[n << 1] = '#';
int i, j;
i = j = 0;
while (i <= n << 1) {
while (i - j >= 0 && i + j <= (n << 1) && t[i - j] == t[i + j]) {
++j;
}
rad[i] = j;
int k = 1;
while (i - k >= 0 && rad[i] - k > rad[i - k]) {
rad[i + k] = rad[i - k];
++k;
}
i += k;
j = max(j - k, 0);
}
}
int FindPa(int x) {
if (pa[x] != x) pa[x] = FindPa(pa[x]);
return pa[x];
}
void Merge(int x, int y) {
x = FindPa(x);
y = FindPa(y);
if (x == y)return;
if (ru[x] < ru[y]) {
pa[x] = y;
ret += 1ll * a[x] * b[y] + 1ll * a[y] * b[x];
a[y] += a[x];
b[y] += b[x];
} else {
pa[y] = x;
ret += 1ll * a[x] * b[y] + 1ll * a[y] * b[x];
a[x] += a[y];
b[x] += b[y];
if (ru[x] == ru[y]) ++ru[x];
}
}
void AddA(int x) {
x = FindPa(x);
ret += b[x];
++a[x];
}
void AddB(int x) {
x = FindPa(x);
ret += a[x];
++b[x];
}
int main() {
// freopen("/Users/yogy/acm-challenge-workbook/db.in", "r", stdin);
scanf("%s", s);
ns = strlen(s);
s[ns] = '|';
scanf("%s", s + ns + 1);
n = strlen(s);
GaoSa();
GaoLcp();
Manacher();
// odd
ret = 0;
for (int i = 0; i <= n; i++) pa[i] = i;
memset(ru, 0, sizeof(ru));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i < n; ++i) {
q[lcp[i]].push_back(i);
in[rad[i << 1 | 1] - 1].push_back(i);
}
int m = n;
if (m % 2 == 0) --m;
for (int i = n; i > (m + 1) >> 1; --i) {
for (auto &j: q[i]) {
Merge(sa[j], sa[j + 1]);
}
}
for (int i = m; i >= 1; i -= 2) {
for (auto &j : in[i]) {
if (j < ns) AddA(j);
else if (j > ns) AddB(j);
}
for (auto &j: q[(i + 1) >> 1]) {
Merge(sa[j], sa[j + 1]);
}
ans += ret;
}
//even
ret = 0;
for (int i = 0; i <= n; i++) pa[i] = i;
memset(ru, 0, sizeof(ru));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i <= n; ++i) {
in[i].clear();
}
for (int i = 0; i <= n << 1; i += 2) {
in[rad[i] - 1].push_back(i >> 1);
}
if (m % 2 == 1) --m;
for (int i = n; i > m >> 1; --i) {
for (auto &j: q[i]) {
Merge(sa[j], sa[j + 1]);
}
}
for (int i = m; i >= 2; i -= 2) {
for (auto &j : in[i]) {
if (j < ns) AddA(j);
else if (j > ns) AddB(j);
}
for (auto &j: q[i >> 1]) {
Merge(sa[j], sa[j + 1]);
}
ans += ret;
}
cout << ans << endl;
return 0;
}