Problem description
Xiao Ming is playing a "coin-flipping" game.
There are a number of coins lined up on the table. We use * to denote the positive side, and O for the opposite side (lowercase letters, not 0).
For example, the possible scenario is: **oo***oooo
If you flip the two coins on the left at the same time, change to: oooo***oooo
Now Xiao Ming's question is: if you know the initial state and to achieve the target State, can only flip the adjacent two coins at a time, then the specific situation, at least how many times to flip?
We agreed: Turn the next two coins called one-step operation, then ask:
Input format
Two lines of equal length string representing the initial state and the target state to be reached. Length of each line <1000
Output format
An integer that represents the minimum number of operation steps.
Sample Input 1
o****o****
Sample Output 1
5
Sample Input 2
o**o***o**
o***o**o**
Sample Output 2
1
Ideas:
The topic hints with greed, so just follow the greedy idea, find just the same as the guide
Import Java.util.Scanner; Public classTest { Public Static void Main(string[] args) {Scanner input =NewScanner (System.inch); String str1 = Input.next (); String str2 = Input.next ();Char[] arr1 = Str1.tochararray ();Char[] arr2 = Str2.tochararray ();intCount =0; for(inti =0; I < arr2.length-1; i++) {if(Arr1[i]! = Arr2[i]) {Arr1[i] = Arr2[i];if(arr1[i+1] ==' * ') {arr1[i+1] =' O '; }Else{arr1[i+1] =' * '; } count++; }} System. out. println (count); }}
Add:
Previously wanted to replace the replacement with three mesh operations, has been the three mesh operator as if the shorthand, did not expect to have been error, searched, only to find the following problem, must have a value to accept the expression.
Ternary operation:
result = (expression)? A:B;
No result, ternary operation will error
Blue Bridge Cup--flip coin