试题 历届试题 翻硬币

资源限制
时间限制:1.0s   内存限制:256.0MB
问题描述

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

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

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

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

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

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

输入格式

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

输出格式

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

样例输入1
**********
o****o****
样例输出1
5
样例输入2
*o**o***o***
*o***o**o***
样例输出2
1
#include<iostream>//输入输出 
#include<algorithm>//常用函数2 
#include<vector> //变长数组 
#include<cstring>//字符串操作 
#include<string>//字符串 
#include<queue>//队列 
#include<map>//map集合 
#include<cmath>//数学 
#define OK 1
#define ERROR 0
#define MAX 100020
const double eps=1e-5;
const int maxn=1010;
const int MAXV=100;
const int INF=1000000000;
typedef long long LL;
const int P=10000019;
const int mod=1000000007;
using namespace std;

int a[maxn];
int b[maxn];

int Change(string st,string bt){
    int n=st.size();
    int m=bt.size();
    int sum=0;
    for(int i=0;i<n-1;i++){
        if(st[i]!=bt[i]){
            if(st[i]=='*'){
                st[i]='o';
            }else if(st[i]=='o'){
                st[i]='*';
            }
            if(st[i+1]=='*'){
                st[i+1]='o';
            }else if(st[i+1]=='o'){
                st[i+1]='*';
            }
            sum++;
        }
    }
    return sum;
}


int main(){
    string st,bt;
    cin>>st>>bt;
    int count=Change(st,bt);
    printf("%d
",count);
    return 0;
}
原文地址:https://www.cnblogs.com/dreamzj/p/13710859.html