Blue Bridge Cup - flip a coin (recursive)

Note: The fourth title from Blue Bridge Cup tournament province C / C ++ Group B

Face questions as follows:

Xiao Ming is playing a "coin flip" game.

Lying on the table a number of coins arranged in a row. We * indicates positive, negates the use o (lowercase letters, not zero).

For example, it may be the case:**oo***oooo

If both left flip two coins, then becomes:oooo***oooo

Xiao Ming question now is: If you know the initial and target states to be achieved, while you can only flip two adjacent coins, then the particular situation, at least to flip it how many times?

We agreed: the two adjacent flip a coin called the step of the operation.

Input Format

Two rows long string, etc., represent the initial state and the target state to be achieved.

Output Format

An integer indicating the minimum number of operation steps

data range

The length of the input string not exceeding 100.
The answer must ensure data solvable.

Sample Input 1:

**********
o****o****

Output Sample 1:

5

Sample Input 2:

*o**o***o***
*o***o**o*** 

Output Sample 2:

1

This question is very much like a problem "inexplicable switch", but a lot less than the amount of code that question, this question my whole idea is as follows: First, let's correspondence relationship shown below with an adjacent coin flip will operate attributed to the node:
(we assume this is the beginning point of a sequence of coins)


Next we define an operation called "flip operation."
We require: If a "flip operation" a place occurs, we will change the pros and cons of a and b are two coins; if a "flip operation" at b occurs, we will change the positive and negative b and c of two coins , so
attention should no longer have the e "reversing operation", since only the adjacent coins e d coin, while changing d and e are positive and negative two coins occurring at d 'reversing operation "is defined as Therefore the figure above, may actually happen "flipping operation" only at the N-1 in
the figure above, we can see if the coin at the pros and cons of a state needs to change, we have to perform at a coin "flip operation ", because no other parts of" reversing operation "capable of changing a state of the coin at
once finalized at a" reversing operation "do or do not, in addition to b at the" outside reversing operation ", there is no other local "flip operation" can change the coin at the state b, and so on, we can always analogy to d at the
conclusion: "operation flip" i do not depend at "flip i 1-operation at "after, i at Whether state money correctly; that is, "flipping operation" at i do and not do to ensure that the coins out of state i at the proper consideration
and for e coins at the end (it is the first N coins, and our "flipping operation" , only to 1 N-at first), there is no guarantee behind its status correctly
so if full consideration, we should determine what e of the coin state is correct: If correct, we will be able to flip a coin to the target state ; otherwise can not (but subject to ensure solvable, so save trouble hhh)

code is very simple: O (N) swept family to stay

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    char c1[105];
    char c2[105];
    cin >> c1;
    cin >> c2;
    int length = strlen(c1);
    int cnt = 0;
    for(int i = 0; i < length - 1; i++) {
        if(c1[i] != c2[i]) {
            cnt++;
            if(c1[i+1] == '*') c1[i+1] = 'o';
            else c1[i+1] = '*';
        }
    }
    cout << cnt << endl;
    return 0;
}

 




Guess you like

Origin www.cnblogs.com/Briddle-ch/p/12669539.html