#include <bits/stdc++.h>
#define maxn 1000005
#define INF 1000020000
using namespace std;
int n;
string a,b;
int dp[maxn][2][2];
int minn(int x)
{
return min(min(dp[x][0][0],dp[x][0][1]), min(dp[x][1][0],dp[x][1][1]));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
cin>>a;
cin>>b;
if(a[0]==b[0])
{
dp[0][0][0]=0;
dp[0][0][1]=INF;
dp[0][1][0]=1;
dp[0][1][1]=2;
}
else
{
dp[0][0][0]=INF;
dp[0][0][1]=1;
dp[0][1][0]=1;
dp[0][1][1]=2;
}
for(int i=1;i<n;i++)
{
if(a[i]==b[i])
{
dp[i][0][0]=minn(i-1);
dp[i][0][1]=INF;
dp[i][1][0]=minn(i-1)+1;
if(b[i-1]==b[i])dp[i][1][0]=min(dp[i][1][0],dp[i-1][1][0]);
else dp[i][1][0]=min(dp[i][1][0],dp[i-1][1][1]);
dp[i][1][1]=minn(i-1)+2;
if(b[i]==b[i-1])dp[i][1][1]=min(dp[i][1][1],dp[i-1][1][1]);
if(b[i-1]+b[i]==1)dp[i][1][1]=min(dp[i][1][1],dp[i-1][1][0]+1);
dp[i][1][1]=min( min(dp[i-1][0][1],dp[i-1][1][1])+1, dp[i][1][1]);
}
else
{
dp[i][0][0]=INF;
dp[i][0][1]=min( min(dp[i-1][0][1],dp[i-1][1][1]), min(dp[i-1][0][0],dp[i-1][1][0])+1 );
dp[i][1][0]=minn(i-1)+1;
if(b[i-1]==b[i])dp[i][1][0]=min(dp[i][1][0],dp[i-1][1][0]);
else dp[i][1][0]=min(dp[i][1][0],dp[i-1][1][1]);
dp[i][1][1]=minn(i-1)+2;
if(b[i]==b[i-1])dp[i][1][1]=min(dp[i][1][1],dp[i-1][1][1]);
if(b[i-1]+b[i]==1)dp[i][1][1]=min(dp[i][1][1],dp[i-1][1][0]+1);
dp[i][1][1]=min( min(dp[i-1][0][1],dp[i-1][1][1])+1, dp[i][1][1]);
}
}
/*for(int i=0;i<n;i++)
{
cout<<i<<": "<<dp[i][0][0]<<" "<<dp[i][0][1]<<" "<<dp[i][1][0]<<" "<<dp[i][1][1]<<endl;
}*/
cout<<minn(n-1)<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
388 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
404 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
388 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
404 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
27 ms |
20044 KB |
Output is correct |
8 |
Correct |
27 ms |
20040 KB |
Output is correct |
9 |
Correct |
27 ms |
20040 KB |
Output is correct |
10 |
Correct |
27 ms |
20044 KB |
Output is correct |
11 |
Correct |
26 ms |
20008 KB |
Output is correct |
12 |
Correct |
27 ms |
20036 KB |
Output is correct |
13 |
Correct |
27 ms |
20040 KB |
Output is correct |
14 |
Correct |
27 ms |
20128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
388 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
404 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |