Algorithmic note _194: Previous questions flipping coins (Java)

Source: Internet
Author: User
Tags ming

Directory

1 Problem Description

2 Solutions

  1 problem description 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 15Sample Input 2*o**o***o***
*o***o**o***Sample Output 21

2 Solutions

The specific code is as follows:

ImportJava.util.Scanner; Public classMain { Public Static voidReverseChar[] A,inti) {if(A[i] = = ' O ') A[i]= ' * '; Else if(A[i] = = ' * ') A[i]= ' O '; }         Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); String A=In.next (); String B=In.next (); intresult = 0; Char[] Arraya =A.tochararray (); Char[] Arrayb =B.tochararray ();  for(inti = 0;i < arraya.length-1;i++) {            if(Arraya[i]! =Arrayb[i])                {Reverse (Arraya, i); Reverse (Arraya, I+ 1); Result++;    }} System.out.println (Result); }}

Algorithmic note _194: Previous questions flipping coins (Java)

Related Article

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.