翻硬币
小明正在玩一个“翻硬币”的游戏。
桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。
比如,可能情形是:oo*oooo
如果同时翻转左边的两个硬币,则变为:oooo***oooo
现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?
我们约定:把翻动相邻的两个硬币叫做一步操作,那么要求:
程序输入:
两行等长的字符串,分别表示初始状态和要达到的目标状态。每行的长度<1000
程序输出:
一个整数,表示最小操作步数
例如:
用户输入:
**********
o****o****
程序应该输出:
5
再例如:
用户输入:
*o**o***o***
*o***o**o***
程序应该输出:
1
资源约定:
峰值内存消耗 < 64M
CPU消耗 < 1000ms
提交时,注意选择所期望的编译器类型。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
int s[1010];
int g[1010];
string str;
int main()
{
ios::sync_with_stdio(false);
cin >> str;
for(int i = 0; i < str.length(); i++)
if(str[i] == '*') s[i] = 1;
cin >> str;
for(int i = 0; i < str.length(); i++)
if(str[i] == '*') g[i] = 1;
int sum = 0;
for(int i = 0; i < str.length() - 1; i++)
{
if(s[i] == g[i]) continue;
sum++;
s[i + 1] ^= 1;
}
cout << sum;
}