이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define rando mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi<<","<<x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ld long double
#define ll long long
//#define int ll
#define ii pair<int,int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int> >
#define mp make_pair
#define INF 1000000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
int t[1000002],pre[1000002],dp[1000002];
int n;
string A,B;
void pro(){
for (int i = n-1; i >= 0; i--)
{
int h=(int)(A[i]-'0')^(int)(B[i]-'0');
if(!h)t[i]=-1;
else if(i==(n-1)||t[i+1]==-1)t[i]=i;
else t[i]=t[i+1];
if(i==(n-1)||(B[i]!=B[i+1]))pre[i]=i;
else pre[i]=pre[i+1];
dp[i+1]=INF;
}
}
int solve(){
dp[0]=0;
for (int i = 0; i < n; ++i)
{
if(A[i]==B[i])dp[i+1]=min(dp[i+1],dp[i]);
else{
dp[t[i]+1]=min(dp[t[i]+1],dp[i]+1);
dp[pre[i]+1]=min(dp[pre[i]+1],dp[i]+1);
}
}
return dp[n];
}
int32_t main(){
boost;
//freopen(".in","r",stdin);
cin>>n;
cin>>A>>B;
pro();
int ans=solve();
for (int i = 0; i < n; ++i)
A[i]=(char)(1-(int)(A[i]-'0')+'0'),pre[i]=t[i]=0;
pro();
cout<<min(ans,solve()+1);
return 0;
}
//long long
//array bounds
//special cases
//binary search
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |