fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc = new Scanner(System.in);
  14. int n = sc.nextInt(); sc.nextLine();
  15. String s1 = sc.nextLine();
  16. String s2 = sc.nextLine();
  17.  
  18. int[] dp = new int[n];
  19.  
  20. if(s1.charAt(0) == s2.charAt(0)){
  21. dp[0] = 0;
  22. }else{
  23. dp[0] = 1;
  24. }
  25.  
  26. System.out.println(dp[0]);
  27.  
  28.  
  29. if(s1.charAt(1) == s2.charAt(1)){
  30. dp[1] = dp[0];
  31. }else{
  32. dp[1] = 1 + dp[0];
  33.  
  34. int ch = 0;
  35. if(s1.charAt(1) != s1.charAt(0)){
  36. ch++;
  37. }
  38. if(s2.charAt(1) != s2.charAt(0)){
  39. ch++;
  40. }
  41. dp[1] = Math.min(dp[1], ch);
  42. }
  43. System.out.println(dp[1]);
  44.  
  45. for(int i = 2; i < n; i++){
  46. int cv = 0;
  47. int ch = 0;
  48.  
  49. if(s1.charAt(i) != s2.charAt(i)){
  50. cv++;
  51. }
  52.  
  53. if(s1.charAt(i) != s1.charAt(i-1)){
  54. ch++;
  55. }
  56. if(s2.charAt(i) != s2.charAt(i-1)){
  57. ch++;
  58. }
  59.  
  60. dp[i] = Math.min(cv + dp[i-1] , ch + dp[i-2]);
  61.  
  62. System.out.println(dp[n-1]);
  63. }
  64. }
  65. }
Success #stdin #stdout 0.11s 54500KB
stdin
5
RBRBR
BBBRB
stdout
1
1
0
0
4