Flip the coin【String| Thinking】

Flip the coin

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 and o to represent the back (lowercase letters, not zeros).

For example, the possible situation is: **oo***oooo

If you flip the two coins on the left at the same time, it becomes: oooo***oooo

Now Xiao Ming’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 particular situation?

We agree: flipping two adjacent coins is called a one-step operation, then the requirements:

Input

Two strings of equal length represent the initial state and the target state to be reached. The length of each line <1000

Output

An integer representing 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

Solution: After pushing for a long time, the number of flips is the difference between the neighbors.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <climits>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
#include <queue>
#include <algorithm>
#include <map>
#include <cstdlib>
using namespace std;
#define inf 0x3f3f3f3f
const int N = 1000005;
string s,t;

int main()
{
    cin >> s >> t;
    int len = s.size();
    int first = -1;
    int ans = 0;
    for(int i = 0; i < len; i ++){
        if(s[i] != t[i] && first == -1) {
            first = i;
        }
        else if(s[i] != t[i]){
            ans += i - first;
            first = -1;
        }
    }
    cout << ans <<endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/Mercury_Lc/article/details/107237348