QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#444902 | #8521. Pattern Search II | ucup-team1198# | WA | 0ms | 159180kb | C++20 | 2.2kb | 2024-06-15 22:09:48 | 2024-06-15 22:09:48 |
Judging History
answer
#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int MAXN = 2e5, MAXLN = 100;
int rgh[MAXLN][MAXN];
int lft[MAXLN][MAXN];
int len[MAXLN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
len[0] = len[1] = 1;
for (int i = 2; i < MAXLN; ++i) {
len[i] = len[i - 1] + len[i - 2];
if (len[i] >= (int)1e6) break;
}
string t;
cin >> t;
int n = t.size();
if (n == 1) {
cout << "1\n";
return 0;
}
for (int i = 0; i < n; ++i) {
rgh[0][i] = i + (t[i] == 'b');
rgh[1][i] = i + (t[i] == 'a');
}
for (int i = 0; i < MAXLN; ++i) {
rgh[i][n] = n;
}
int lvl;
for (lvl = 2; rgh[lvl - 1][0] < n; ++lvl) {
for (int i = 0; i < n; ++i) {
rgh[lvl][i] = rgh[lvl - 2][rgh[lvl - 1][i]];
}
}
int maxr = lvl;
auto go_right = [&](int i) {
int ans = 0;
for (int lvl = maxr - 1; lvl >= 1; --lvl) {
if (rgh[lvl][i] < n) {
ans += len[lvl];
i = rgh[lvl][i];
}
}
return ans + 1;
};
for (int i = 1; i <= n; ++i) {
lft[0][i] = i - (t[i - 1] == 'b');
lft[1][i] = i - (t[i - 1] == 'a');
}
for (int i = 0; i < MAXLN; ++i) {
lft[i][0] = 0;
}
for (lvl = 2; lft[lvl - 2][n] > 0; ++lvl) {
for (int i = 1; i <= n; ++i) {
lft[lvl][i] = lft[lvl - 1][lft[lvl - 2][i]];
}
}
int maxl = lvl;
auto go_left = [&](int i, int r) {
int start = maxl - 1;
if (start % 2 != r) --start;
int ans = 0;
for (int lvl = start; lvl >= 0; --lvl) {
if (lft[lvl][i] > 0) {
ans += len[lvl];
i = lft[lvl][i];
} else {
--lvl;
}
}
return ans + 1;
};
int ans = 3 * n;
for (int i = 1; i < n; ++i) {
for (int r = 0; r < 2; ++r) {
int p = go_left(i, r);
int q = go_right(i);
ans = min(ans, p + q);
}
}
cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 157104kb
input:
aabbaab
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
a
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
b
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 157108kb
input:
aa
output:
2
result:
ok 1 number(s): "2"
Test #5:
score: 0
Accepted
time: 0ms
memory: 157152kb
input:
bb
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 159180kb
input:
ab
output:
3
result:
wrong answer 1st numbers differ - expected: '2', found: '3'