QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#559960 | #8223. 字符串游戏 | Fido_Puppy | 15 | 805ms | 323640kb | C++23 | 3.8kb | 2024-09-12 10:57:01 | 2024-09-12 10:57:01 |
Judging History
answer
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define rz resize
#define MP make_pair
#define MT make_tuple
#define IT iterator
#define fi first
#define se second
#define For(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define Rep(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define CLR(a, v) memset(a, v, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define debug cerr << "ztxakking\n"
#define y0 ztxaknoi
#define y1 ztxakioi
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using vi = vector<int>;
template<typename T>
using V = vector<T>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e6 + 7;
int n, f[N];
string s;
struct FenwickTree {
int t[N];
void add(int x, int k) {
for (; x < N; x += x & -x) t[x] += k;
}
int qry(int x) {
int ans = 0;
for (; x; x -= x & -x) ans += t[x];
return ans;
}
} T;
struct Graph {
struct edge {
int to, nxt;
} e[N];
int hd[N], tot;
void add(int u, int v) {
e[++tot] = (edge){v, hd[u]}, hd[u] = tot;
}
};
struct SAM {
Graph G;
int dfn[N], hson[N], sz[N], dep[N], top[N], seg[N], rev[N], timer, tot;
int nxt[N][26], lnk[N], len[N], pos[N], cnt, lst;
int clone(int x) { ++cnt, lnk[cnt] = lnk[x], CPY(nxt[cnt], nxt[x]); return cnt; }
void extend(int c) {
int cur = ++cnt, p = lst; len[cur] = len[p] + 1;
for (; p && !nxt[p][c]; p = lnk[p]) nxt[p][c] = cur;
if (!p) lnk[cur] = 1;
else {
int q = nxt[p][c];
if (len[q] == len[p] + 1) lnk[cur] = q;
else {
int r = clone(q); len[r] = len[p] + 1;
for (; p && nxt[p][c] == q; p = lnk[p]) nxt[p][c] = r;
lnk[cur] = lnk[q] = r;
}
}
lst = cur;
}
void dfs1(int u) {
sz[u] = 1, dfn[u] = ++timer, dep[u] = dep[lnk[u]] + 1;
for (int i = G.hd[u]; i; i = G.e[i].nxt) {
int v = G.e[i].to;
dfs1(v), sz[u] += sz[v];
if (sz[hson[u]] < sz[v]) hson[u] = v;
}
}
void dfs2(int u) {
seg[u] = ++tot, rev[tot] = u;
if (hson[u]) {
top[hson[u]] = top[u];
dfs2(hson[u]);
}
for (int i = G.hd[u]; i; i = G.e[i].nxt) {
int v = G.e[i].to;
if (v == hson[u]) continue;
top[v] = v, dfs2(v);
}
}
int find(int l, int r) {
int L = r - l + 1, u = pos[r], ans = u;
while (u) {
if (len[top[u]] >= L) ans = top[u], u = lnk[top[u]];
else {
int l = seg[top[u]], r = seg[u];
while (l <= r) {
int mid = l + r >> 1;
if (len[rev[mid]] >= L) ans = rev[mid], r = mid - 1; else l = mid + 1;
}
break;
}
}
return ans;
}
void ins(int r) {
int u = pos[r];
T.add(dfn[u], 1);
}
int occ(int l, int r) {
int u = find(l, r);
return T.qry(dfn[u] + sz[u] - 1) - T.qry(dfn[u] - 1);
}
void init(string s) {
lst = cnt = 1;
For(i, 1, n) extend(s[i] - 'a'), pos[i] = lst;
For(i, 2, cnt) G.add(lnk[i], i);
dfs1(1), top[1] = 1, dfs2(1);
}
} S;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> s, n = s.size(), reverse(all(s)), s = '@' + s, S.init(s);
vi stk = {0};
For(i, 1, n) {
S.ins(i);
while (stk.size() > 1) {
int x = stk.back(), y = stk[stk.size() - 2];
if (S.occ(x + 1, i) - f[x] <= S.occ(y + 1, i) - f[y]) stk.pop_back();
else break;
}
int u = stk.back();
f[i] = S.occ(u + 1, i) - f[u];
stk.pb(i);
}
cout << f[n] << '\n';
cerr << (double)(clock()) / CLOCKS_PER_SEC << '\n';
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 10
Accepted
time: 0ms
memory: 21604kb
input:
abbabbbbabbaaaaabbbaabaaaaaabbbaabbabababaabababaababaaaaaabaaabaaabbbbababaaabbbabababbbabbbaabaaababbbaabaabbaaabbababaaaaabbbaabaaabbaabaabaaabaaabaababaabbbbbbbbabbbbbabbbaabababbaababbbbbabbbbaaabbaabaabaaabbabbaaabbaaababaabbbaabbbaaaabbababbabaababababaaaabbaaaabaabaaaababaaabababbbaaabababbb...
output:
1281
result:
ok single line: '1281'
Test #2:
score: 10
Accepted
time: 2ms
memory: 21916kb
input:
aaaabababbabbbbbabbbbbbbbbbbabbbbbabbbaaabbbbaababaabbbbbbbbbbbbbabaabbaaabaababaabaaabbabbbabbbabaabbaabaababbbbbbaaabbbbaaabaabbbabbbabbabaabbbbabbbaabaaaabbbbbabaaaabaabbabaaabbaabbabbaaabbabaaababbababababaabaaaabaabaabaaabaabbbabbbbbaababbabbbabbbbababaaabbabbaaaabbbaaabaabbbaaaaaababaababbbbba...
output:
1483
result:
ok single line: '1483'
Test #3:
score: 0
Wrong Answer
time: 5ms
memory: 22856kb
input:
bbaaababbbabbbaabbbbbbababbbabbbbbbbbaabbbbbbbbbbaaabbbbbbbbbbbbbbbabbabbbabaababbbabbbbabbabbabbbbabbbbbbbbbaaababbaaabbbaabbabbbbbbbbbbbbbbbbbbbbabbbbbbababababbbbbbbbbabababaabbbbbbabbbbaabbababbaaababbbbabbbbabbbabbbbbabbbbbabbbbbbbbbbabbaabbbabababbbbaababbbababbbbabbabbaabbbbbbabbbbbababbbbaba...
output:
2214
result:
wrong answer 1st lines differ - expected: '2310', found: '2214'
Subtask #2:
score: 10
Accepted
Test #11:
score: 10
Accepted
time: 716ms
memory: 322392kb
input:
aaaaaaabbaaabbbbabbabbbababaabaaabaabbaaababaaaaaabbbbbaabbbabbbaabbabaababaababaabbbababbbabaaaaaaaaabaaaaabbbabbbbbabaaabaaaababbabbbbbbaabbbbbaababbbbababbbbbbbbbbbaaabbabbaababbaaaabbabaaabbaaabbbbaaababbbbbabbaababbaaababaaabbabaabbbbabbaaabbababbaabbaaaaaaaaaabababbbaaaababbabbbabbababbabbabbb...
output:
234567
result:
ok single line: '234567'
Test #12:
score: 10
Accepted
time: 765ms
memory: 322204kb
input:
aababbbbaaabaabababbbaabbabbaabbaababaabaabaabbbaaaabbabbbbbaaaaabbbaabbbaaaaaaaabbbabaabbbaabaaabaabbabbabbbaaabbbbbaabaabaaaaabbbabbaaaababbaaababbbbaaabaabbababbbbbaaabbaabaaaaaaabbbbbaabbaaabaababababbbbabbbbaabbababbbbbaabbabaaaaabbabbbabbaaaababababbbabaabababaaabbbbaaabbabababaabaabaaabbabaab...
output:
198433
result:
ok single line: '198433'
Test #13:
score: 10
Accepted
time: 791ms
memory: 323640kb
input:
bababbbabaaabbbaabaabaababbbaabaabaaaaaabaaaaaaaabaaabbabbaaaaababbbbbbababaabbaabbabaabaaabaaaabbbbbaaabbababbabbbabbbaaaabbbbaaabbaabbbababbbabbbbabbbbabbbbabaaaabaaaabababbaaabbaaabbaabbbbbababababbbbbbbbabaaabaaaaaaabbbabbabbbaaabbbabbababaabababaabbabbbabaaabbaabbabbbababaababaaabaabababaababbb...
output:
248857
result:
ok single line: '248857'
Test #14:
score: 10
Accepted
time: 805ms
memory: 323584kb
input:
aabababababbaaaababbaabbabaaaaaababbaabaaabbaaaaabaaabbaababbbaaaabbbaabbabbaaaabaaabbbaaaababbbbaabaabbaabaaaaaabababaabbaaabbbaaabbabababaabaaaababaaaaabbbbabbaabbbababbbbaaaaabbbabaabbbaababaabbaaababbaabababbabaaabbbabbbbabaaabaaabbbaabbbaabbbabbbbabaaabbabababbbbabababbbbbbaaababaababaabaabbbbb...
output:
257032
result:
ok single line: '257032'
Test #15:
score: 10
Accepted
time: 726ms
memory: 322328kb
input:
bbbababbaaabbaababaaababaaaababababbbbbabaaaaaabbbbbabbabbaaabaaabababbbabbbaaaaaaaaabbaaaababbbaabaaabbaaabbabbabaabbabbbaabbabaabbbbaaabbbaaabbabbbabbabbbbaabaababaaabbbbabbbbbaaaaaaabbabbbbbabbabbbbaaabbaabbbaabbbbaabbbbbbbababaabaaaabbababaaaaabbabaaabaabbaabbbaaabbbbbbaabababbbbbababbbaabbbabaa...
output:
222902
result:
ok single line: '222902'
Subtask #3:
score: 5
Accepted
Test #16:
score: 5
Accepted
time: 164ms
memory: 261476kb
input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
493827
result:
ok single line: '493827'
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 25
Accepted
time: 49ms
memory: 67252kb
input:
bbbbbbbbbaabbbbbabaaabaabaaabaaaaabaaaabbbbbbbaaaabaabbbababbaaaabbabababbbabbbbabbabaabaaabbabbaaababaaabaaaabababbbbbbbaabbaaabbabbbbbabbbaaaaababbbaaabaabababbabbabababbabbabaaaaabaaaaaabbbabaabbbaaaabbabababaaababaabbbbbbbaaabbbabaabaaaabaabaababbaaaabababaabbbaaaababaaababbbbbbabbbbbbaabbabbaaa...
output:
43524
result:
ok single line: '43524'
Test #18:
score: 0
Wrong Answer
time: 62ms
memory: 76708kb
input:
baaaabbbabbbbabbbbabbbabbbbbabbbabbaabbbbbbabbabaabbbbbbbbbbbbaabbbbbaaabaaabbbbbaabbbbbbbbbbbbbababbbbabbbbbbbbbbbbbbbbabbbbbbbbabbbbabbbbaabaabbbabbbabbbaabbbbbabbbbbbaaabbbbbbaaabbaaabbabbabbbbbaabbbbbabbabababbbbbbbbbbbbabbbbaabbbbabbbbbbbbbbbbbbbabbbbbabbabbabababbbabbbbaabbabbbbbbbbaabbaabbbbb...
output:
127539
result:
wrong answer 1st lines differ - expected: '107148', found: '127539'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%
Subtask #6:
score: 0
Skipped
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #5:
0%