翻硬币---贪心

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

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

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

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

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

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

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

输出格式
一个整数,表示最小操作步数。

样例输入1


o****o****
样例输出1
5
样例输入2
o**o***o**
o***o**o**
样例输出2
1

#include "iostream"
#include "algorithm"
#include "vector"
#include "set"
#include "string.h"
#include "fstream"
#include "string"
using namespace std;

string start, end;

int main()
{
    cin >> start >> end;
    int cnt = 0;
    if(start == end)
        cout << 0;
    else
    {
        int n = start.length();
        for(int i=0; i<n; i++)
        {
            if(start[i] == end[i])
                continue;
            else
            {
                cnt++;
                if(start[i] == 'o')
                    start[i] = '*';
                else
                    start[i] = 'o';
                if(start[i+1] == 'o')
                    start[i+1] = '*';
                else
                    start[i+1] = 'o';
            }
        }
    }
    cout << cnt;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值