// +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+ \\
/* Some Makoto Shinkai's :
“Who cares if we can't see any sunshine? I want you more than any blue sky!!!”
- Tenki no Ko
"By the time the date is over, the comet will be visible in the sky."
- Kimi no Nawa
“No matter what happens, even if the stars fall, I will live.”
- Byōsoku 5 Centimeter
*/
#include <bits/stdc++.h>
#define Raveluk ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define tii tuple<int,int,int>
#define g1 get<0>
#define g2 get<1>
#define g3 get<2>
#define qf q.front()
#define all(x) (x).begin(), (x).end()
using namespace std;
int main()
{
Raveluk
int n,i,j,ans=1e9,k;
string a,b;
cin>>n>>a>>b;
a = '.'+a;
b = '.'+b;
int dp[n+1][3][2];
for(i=0;i<=n;i++){
for(j=0;j<3;j++){
for(k=0;k<2;k++) dp[i][j][k] = 1e9;
}
}
dp[0][0][0] = 0;
// 0 ga ngapa ngapain
// 1 on
// 2 off
for(i=1;i<=n;i++){
int mn = 1e9;
//cout<<i<<" :\n";
for(j=0;j<3;j++){
for(k=0;k<2;k++) mn = min(mn,dp[i-1][j][k]);
}
if(a[i] == b[i]) dp[i][0][0] = mn;
if(a[i] != b[i]){
dp[i][0][1] = mn+1;
for(j=0;j<3;j++) dp[i][0][1] = min(dp[i][0][1],dp[i-1][j][1]);
//cout<<dp[i][0][1]<<" : 0 1\n";
}
if(b[i] == '1'){
// 1 gak invers
// 2 invers
dp[i][1][0] = min(mn+1,dp[i-1][1][0]);
dp[i][1][0] = min(dp[i][1][0],dp[i-1][1][1]);
//cout<<dp[i][1][0]<<" : 1 0\n";
dp[i][2][1] = min(mn+2,dp[i-1][2][1]);
for(j=0;j<3;j++) dp[i][2][1] = min(dp[i][2][1],dp[i-1][j][1]+1);
dp[i][2][1] = min(dp[i][2][1],dp[i-1][2][0]+1);
//cout<<dp[i][2][1]<<" : 2 1\n";
}
if(b[i] == '0'){
// 1 invers
// 2 no invers
dp[i][2][0] = min(mn+1,dp[i-1][2][0]);
dp[i][2][0] = min(dp[i][2][0],dp[i-1][2][1]);
//cout<<dp[i][2][0]<<" : 2 0\n";
dp[i][1][1] = min(mn+2,dp[i-1][1][1]);
// invers ga bayar
for(j=0;j<3;j++) dp[i][1][1] = min(dp[i][1][1],dp[i-1][j][1]+1);
// 1 ga bayar
dp[i][1][1] = min(dp[i][1][1],dp[i-1][1][0]+1);
//cout<<dp[i][1][1]<<" : 1 1\n";
}
}
for(i=0;i<3;i++){
for(j=0;j<2;j++) ans = min(ans,dp[n][i][j]);
}
cout<<ans;
return 0;
}
# | 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... |