QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#282972 | #7779. Swiss Stage | Loging# | WA | 2ms | 9932kb | C++20 | 3.8kb | 2023-12-13 16:18:19 | 2023-12-13 16:18:19 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 5;
string S , T;
vector<vector<int>> solve(const string &a , const string &b , int n , int m) {
vector<vector<int>> f(n + 2 , vector<int>(m + 2));
for (int i = n ; i >= 1 ; i--) {
for (int j = m ; j >= 1 ; j--) {
if (a[i] == b[j]) {
f[i][j] = f[i + 1][j + 1] + 1;
} else {
f[i][j] = 0;
}
}
}
return f;
}
const int M = 20000 + 5;
int cnt = 1;
int len[M] , fa[M] , son[M][26];
long long sa[M] , sb[M] , pa[N] , pb[N] , va[N] , vb[N];
vector<int> G[M];
int extend(int c , int last) {
if (son[last][c]) {
int p = last , q = son[p][c];
if (len[p] + 1 == len[q]) return q;
else {
int nq = ++cnt; len[nq] = len[p] + 1;
copy(son[q] , son[q] + 26 , son[nq]);
fa[nq] = fa[q] , fa[q] = nq;
for ( ; son[p][c] == q ; p = fa[p]) son[p][c] = nq;
return nq;
}
}
int np = ++cnt , p = last;
last = np , len[np] = len[p] + 1;
for ( ; p and not son[p][c] ; p = fa[p]) son[p][c] = np;
if (p == 0) fa[np] = 1;
else {
int q = son[p][c];
if (len[q] == len[p] + 1) fa[np] = q;
else {
int nq = ++cnt; len[nq] = len[p] + 1;
copy(son[q] , son[q] + 26 , son[nq]);
fa[nq] = fa[q] , fa[np] = fa[q] = nq;
for ( ; son[p][c] == q ; p = fa[p]) son[p][c] = nq;
}
}
return np;
}
void dfs(int u) {
for (int v : G[u]) {
dfs(v);
sa[u] += sa[v];
sb[u] += sb[v];
}
}
void dfs1(int u) {
int wa = 0 , wb = 0;
if (sa[u]) wa = len[u] - len[fa[u]];
if (sb[u]) wb = len[u] - len[fa[u]];
for (int v : G[u]) {
dfs1(v);
va[v] += va[u] + wa;
vb[v] += vb[u] + wb;
}
}
int ja[N][N] , jb[N][N];
void debug(int u , string s) {
cout << u << ' ' << s << ' ' << fa[u] << ' ' << len[u] << endl;
for (int i = 0 ; i < 26 ; i++) {
if (son[u][i]) {
debug(son[u][i] , s + (char)('a' + i));
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(0);
cin >> S >> T;
int n = S.size() , m = T.size() + 1;
S = ' ' + S;
T = ' ' + T;
auto f = solve(S , S , n , n);
auto g = solve(T , T , m , m);
auto h = solve(S , T , n , m);
long long ans = 0;
for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= m ; j++) {
ans += h[i][j];
}
}
int last = 1;
for (int i = 1 ; i <= n ; i++) {
last = extend(S[i] - 'a' , last);
pa[i] = last , sa[last]++;
}
last = 1;
for (int i = 1 ; i <= m ; i++) {
last = extend(T[i] - 'a' , last);
pb[i] = last , sb[last]++;
}
for (int i = cnt ; i > 1 ; i--) {
G[fa[i]].push_back(i);
}
dfs(1) , dfs1(1);
debug(1 , "");
for (int i = 1 ; i <= n ; i++) {
int p = pa[i] , l = i , j = 0;
ja[i][j] = p;
while (l) {
--l , ++j;
if (l == len[fa[p]]) p = fa[p];
ja[i][j] = p;
cout << i << ' ' << j << ' ' << l << ' ' << p << endl;
}
}
// for (int i = 1 ; i <= m ; i++) {
// int p = pb[i] , l = i , j = 0;
// while (l) {
// --l , ++j;
// if (l == len[fa[p]]) p = fa[p];
// jb[i][j] = p;
// cout << i << ' ' << j << ' ' << l << ' ' << p << endl;
// }
// }
auto solvea = [&](int u , int L) {
int ret = va[u];
if (sa[u]) ret += L - len[fa[u]];
return ret;
};
auto solveb = [&](int u , int L) {
int ret = vb[u];
if (sb[u]) ret += L - len[fa[u]];
return ret;
};
cout << ans << endl;
for (int i = 1 ; i <= n ; i++) {
for (int j = i + 2 ; j <= n ; j++) {
int l = f[i][j];
if (l == 0) continue;
cout << i << ' ' << j << ' ' << l << endl;
int L = i , R = min(i + f[i][j] - 1 , j - 1);
int p = ja[L][L - 1] , q = ja[R][L - 1];
cout << ":" << L << ' ' << R << ";" << p << ' ' << q << endl;
ans += solvea(p , j - L) - solveb(q , j - R);
cout << solvea(p , j - L) - solveb(q , j - R) << endl;
}
}
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 9932kb
input:
0 1
output:
1 0 0 1 1 0 1 0 0
result:
wrong answer 1st numbers differ - expected: '4', found: '1'