Blue Bridge Cup--flip coin

Source: Internet
Author: User
Tags ming

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.