#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)
const double EPS = 1e-9;
const int MOD = 1e9+7;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};
void DBG(){cout << "]" << endl;}
template<typename T, typename ...U> void DBG(const T& head, const U... args){ cout << head << "; "; DBG(args...); }
#define dbg(...) cout << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__);
#define chk() cout << "Check at line(" << __LINE__ << ") hit." << endl;
template<class T, unsigned int U>
ostream& operator<<(ostream& out, const array<T, U> &v){out << "["; REP(i, U) out << v[i] << ", "; out << "]"; return out;}
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "["; REP(i, v.size()) out << v[i] << ", "; out << "]"; return out;}
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n; cin >> n;
string a, b;
cin >> a >> b;
vector<array<int, 2>> seg;
REP(i, b.length()){
if(i == 0 || b[i] != b[i - 1])
seg.pb({i, i});
else
seg.back()[1]++;
}
int m = seg.size();
array<int, 2> nl = {INF, INF};
vector<array<int, 2>> dp(m + 1, nl);
dp[0][0] = 0; dp[0][1] = 1;
REP(i, m){
bool all_correct = true;
bool all_reversed = true;
bool pos_change = false; //reversed + good -> here I want to end open toggle
bool pos_change2 = false; // good + reversed; -> here I want to start new open toggle
int pref_eq = 0;
int suf_eq = 0;
int pref_rev = 0;
int suf_rev = 0;
FOR(j, seg[i][0], seg[i][1] + 1, 1){
if(a[j] == b[j])
all_reversed = false;
if(a[j] != b[j])
all_correct = false;
}
FOR(j, seg[i][0], seg[i][1] + 1, 1){
if(a[j] == b[j])
pref_eq++;
else break;
}
FOR(j, seg[i][0], seg[i][1] + 1, 1){
if(a[j] != b[j])
pref_rev++;
else break;
}
FORD(j, seg[i][1], seg[i][0], 1){
if(a[j] == b[j])
suf_eq++;
else break;
}
FORD(j, seg[i][1], seg[i][0], 1){
if(a[j] != b[j])
suf_rev++;
else break;
}
if(pref_rev + suf_eq == (seg[i][1] + 1 - seg[i][0]))
pos_change = true;
if(pref_eq + suf_rev == (seg[i][1] + 1 - seg[i][0]))
pos_change2 = true;
// special transitions
if(all_correct){
dp[i + 1][0] = min(dp[i + 1][0], min(dp[i][0], dp[i][1]));
}
if(all_reversed){
dp[i + 1][1] = min(dp[i + 1][1], min(dp[i][1], dp[i][0] + 1));
}
if(pos_change){
dp[i + 1][0] = min(dp[i + 1][0], min(dp[i][0] + 1, dp[i][1]));
}
if(pos_change2){
dp[i + 1][1] = min(dp[i + 1][1], min(dp[i][0] + 1, dp[i][1] + 1));
}
// general transitions
dp[i + 1][0] = min(dp[i + 1][0], min(dp[i][0] + 1, dp[i][1] + 1));
dp[i + 1][1] = min(dp[i + 1][1], min(dp[i][0] + 2, dp[i][1] + 1));
dp[i + 1][1] = min(dp[i + 1][1], dp[i + 1][0] + 1);
dp[i + 1][0] = min(dp[i + 1][0], dp[i + 1][1]);
}
cout << min(dp[m][0], dp[m][1]) << "\n";
return 0;
}
Compilation message
lamp.cpp: In function 'int main()':
lamp.cpp:19:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
| ^
lamp.cpp:21:18: note: in expansion of macro 'FOR'
21 | #define REP(i,b) FOR(i,0,b,1)
| ^~~
lamp.cpp:64:5: note: in expansion of macro 'REP'
64 | REP(i, b.length()){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
10 ms |
2824 KB |
Output is correct |
8 |
Correct |
35 ms |
18476 KB |
Output is correct |
9 |
Correct |
39 ms |
18724 KB |
Output is correct |
10 |
Correct |
35 ms |
18476 KB |
Output is correct |
11 |
Correct |
36 ms |
18480 KB |
Output is correct |
12 |
Correct |
11 ms |
2824 KB |
Output is correct |
13 |
Correct |
11 ms |
2824 KB |
Output is correct |
14 |
Correct |
12 ms |
2824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |