-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equalize.java
36 lines (34 loc) · 1.03 KB
/
Equalize.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* http://codeforces.com/problemset/problem/1037/C #dynamic-programming #greedy 2 lien ke => nen
* swap (vi cost flip = 2, cost swap = 1)
*/
import java.util.Scanner;
public class Equalize {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String a = sc.next();
String b = sc.next();
int preChangePos = -2;
int cost = 0;
for (int i = 0; i < n; i++) {
if (a.charAt(i) != b.charAt(i)) {
// swap
if (preChangePos == i - 1 && a.charAt(i) != a.charAt(i - 1)) {
// reset
preChangePos = -2;
} else { // flip
cost++;
// update previous change position
preChangePos = i;
}
}
}
/**
* clear way int i = 0; while (i < n) { if (a.charAt(i) != b.charAt(i)) { if (i+1 < n &&
* a.charAt(i+1) != b.charAt(i+1) && a.charAt(i+1) != a.charAt(i)) { // swap cost++; i += 2; }
* else { // flip cost++; i++; } } else { i++; } }
*/
System.out.println(cost);
}
}