AcWing 1208 flip the coin

Title description:

Xiao Ming is playing a "coin flip" game.

There are several coins in a row on the table. We use * to represent the front side and o to represent the back side (lowercase letters, not zeros).

For example, the possible situations are:**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, how many times is the least flipped for a specific situation?

We agree: to flip two adjacent coins is called a one-step operation.

Input format

Two strings of equal length respectively represent the initial state and the target state to be reached.

Output format

An integer representing the minimum number of operation steps

data range

The length of the input string does not exceed 100.
The data guarantees that the answer must be solvable.

Input example 1:

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

Output sample 1:

5

Input example 2:

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

Output sample 2:

1

 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAX = 109;

char a[MAX], b[MAX];

int main()
{
    scanf("%s %s", a, b);

    int l = strlen(a);

    int sum = 0;

    for(int i = 0; i < l; i++)
    {
        if(a[i] != b[i])
        {
            sum ++;
            if(a[i+1] == 'o')
                a[i+1] = '*';
            else
                a[i+1] = 'o';
        }
    }

    printf("%d\n", sum);


    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113251503