翻硬币 【 字符串 | 思维 】

翻硬币

Description

小明正在玩一个“翻硬币”的游戏。

桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。

比如,可能情形是:**oo***oooo

如果同时翻转左边的两个硬币,则变为:oooo***oooo

现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?

我们约定:把翻动相邻的两个硬币叫做一步操作,那么要求:

Input

两行等长的字符串,分别表示初始状态和要达到的目标状态。每行的长度<1000

Output

一个整数,表示最小操作步数。

Sample Input 1 

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

Sample Output 1

5

Sample Input 2 

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

Sample Output 2

1

题解:推了半天,翻转的次数就是相邻不同的差值。

#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;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/107237348