QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#446212 | #8521. Pattern Search II | ucup-team3215 | RE | 0ms | 3788kb | C++20 | 1.1kb | 2024-06-17 01:38:57 | 2024-06-17 01:38:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(0);
string s; cin >> s;
int n = s.size();
string pat = "ba";
vector<int> fib{1, 1};
vector<vector<int>> jmp;
for (int j: {0, 1}) {
jmp.push_back(vector(n, 0));
for (int i = 0; i < n; ++i) jmp[j][i] = i + (s[i] == 'b' - j);
}
for (int more = 1; jmp.back()[0] < n || more--; ) {
fib.push_back(fib.end()[-2] + fib.end()[-1]);
int j = jmp.size() - 1;
jmp.push_back(vector(n, 0));
for (int i = 0; i < n; ++i) jmp[j + 1][i] = jmp[j][i] == n? n: jmp[j - 1][jmp[j][i]];
}
int ans = 1e9;
vector<int> v{fib.size() - 1};
do {
if (v.back() > 1) {
v.push_back(v.back() - 1);
v.end()[-2] -= 2;
continue;
} else if (pat[v.back()] != s[0]) {
v.pop_back();
continue;
}
int l = 0, i = v.size() - 1, j = 0;
while (jmp[v[i]][j] < n) l += fib[v[i]], j = jmp[v[i--]][j];
int c = v[i];
while (c > 1) jmp[c - 1][j] < n? j = jmp[c - 1][j], l += fib[c - 1], c -= 2: --c;
ans = min(ans, l + 1);
v.pop_back();
} while (v.size() > 1);
cout << ans;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3788kb
input:
aabbaab
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
a
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3556kb
input:
b
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3536kb
input:
aa
output:
2
result:
ok 1 number(s): "2"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
bb
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3764kb
input:
ab
output:
2
result:
ok 1 number(s): "2"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
ba
output:
2
result:
ok 1 number(s): "2"
Test #8:
score: -100
Runtime Error
input:
bbba