#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, n_;
string a, b, a_, b_;
int toggle(int p) {
while (p != n_ && a_[p] != 2 && a_[p] != b_[p])
p++;
return p;
}
int find(int l, int r) {
while (l != r && a_[l] == b_[l])
l++;
r--;
while (l <= r && a_[r] == b_[r])
r--;
if (l > r)
return 0;
int change = 0;
for (int i = l; i <= r; i++) {
if (b_[i] != b_[l] && b_[i] != b_[i - 1])
change++;
}
return change + 1;
}
bool check(int p) {
bool ans = true;
int temp = p--;
while (p >= 0 && b_[p] == b_[temp]) {
if (a_[p] != b[p])
ans = false;
p--;
}
return ans;
}
int solve(int lp) {
while (lp != n_ && a_[lp] == b_[lp])
lp++;
if (lp == n_)
return 0;
if (lp == n_ - 1)
return 1;
int last = n_, change = 0;
for (int p = lp; p < n_; p++) {
if (b_[p] != b_[lp] && b_[p] == a_[p] && (p == n - 1 || b_[p] != b_[p + 1]))
return 1 + change + solve(p);
if (b_[p] != b_[lp] && b_[p] != b_[p - 1])
change++;
if (a_[p] == b_[p])
last = n_;
else if (last == n_)
last = p;
if (last < p - 1 && b_[p] != b_[lp] && check(p - 2))
return find(lp, last) + 1 + solve(toggle(last));
}
return last == lp ? 1 : 1 + change;
}
int solveTestCase(int test) {
cin >> n >> a >> b;
for (int i = 0; i < n; n_++) {
int temp = i;
while (temp != n && b[temp] == b[i] && a[temp] == a[i])
temp++;
b_ += b[i];
a_ += a[i];
i = temp;
}
//cout << a_ << endl << b_ << endl;
cout << solve(0);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
for (int i = 1; i <= test; i++)
solveTestCase(i);
}
Compilation message
lamp.cpp: In function 'long long int solveTestCase(long long int)':
lamp.cpp:85:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
3100 KB |
Output is correct |
8 |
Correct |
28 ms |
5648 KB |
Output is correct |
9 |
Correct |
28 ms |
5648 KB |
Output is correct |
10 |
Correct |
28 ms |
5636 KB |
Output is correct |
11 |
Correct |
27 ms |
5580 KB |
Output is correct |
12 |
Correct |
10 ms |
3100 KB |
Output is correct |
13 |
Correct |
10 ms |
3100 KB |
Output is correct |
14 |
Correct |
10 ms |
3100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |