Problem Description
Xiao Ming is playing a "coin flip" game.
There are several coins arranged in a row on the table. We use * for heads and o for tails (lowercase, not zero).
For example, the possible situation is: **oo***oooo
If you flip the two coins on the left at the same time, it becomes: oooo***oooo
Now Xiaoming's question is: If the initial state and the target state to be reached are known, and only two adjacent coins can be flipped at the same time at a time, what is the minimum number of flips for a specific situation?
We agree that flipping two adjacent coins is called a one-step operation, then the requirement:
input format
Two lines of equal-length strings represent the initial state and the target state to be achieved, respectively. length of each line < 1000
output format
An integer representing the minimum number of operation steps.
Sample input 1
o****o****
Sample output 1
5Sample
input 2
o**o***o**
o***o**o**
Sample output 2
1
Solution:
Ideas: Contrast successively, if there is a difference, flip the last digit, compare sequentially, and record the number of flips.
code show as below:
import java.util.Scanner;
/**
* @author 作者 : Cactus
* @version 创建时间:2018-3-27 下午07:58:17
*/
public class Main {
private static int count = 0, length;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String strStart = sc.nextLine();
String strEnd = sc.nextLine();
sc.close();
char[] arrStart = strStart.toCharArray();
char[] arrEnd = strEnd.toCharArray();
length = strStart.length();
for(int i = 0; i < length; i++){
if(arrStart[i] != arrEnd[i]){
count++;
arrStart[i + 1] = flipSingle(arrStart[i + 1]);
}
}
System.out.println(count);
}
private static char flipSingle(char x){
if('o' == x){
return '*';
}else{
return 'o';
}
}
}