Sweety

Practice makes perfect

导航

蓝桥杯---历届试题 翻硬币 (水题)

Posted on 2015-05-05 11:17  蓝空  阅读(227)  评论(0编辑  收藏  举报
问题描述

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

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

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

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

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

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

输入格式

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

输出格式

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

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

这也算贪心?
#include <iostream>  
#include <stdio.h>  
#include <algorithm>  
#include <string.h>  
#include <string>
#include <stdlib.h>  
#include <cmath>  
#include <iomanip>  
#include <vector>  
#include <set>  
#include <map>  
#include <stack>  
#include <queue>  
#include <cctype>  
#define rd(x) scanf("%d",&x)  
#define rd2(x,y)  scanf("%d%d",&x,&y)  
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)  
#define Max 0x3f3f3f3
#define ArrMax 1005 
using namespace std;
int main (){
	string str,str1;
	while(cin>>str>>str1){
		bool map[ArrMax];
		memset(map,0,sizeof(map));
		int len = str.length();
		for(int i=0;i<len;i++)
		  if(str[i]!=str1[i])
		    map[i]=1;
		int sum=0;
		for(int i=0;i<len;i++)
		  if(map[i]==1){
		  	int count=1,j;
		    for(j=i+1;map[j]!=1&&j<len;j++){
		  	  count++;
		    }	
		    i=j;
		    sum+=count;
		  }
		  cout<<sum<<endl;	
	}
	return 0;
}