zoukankan      html  css  js  c++  java
  • 历届试题 翻硬币

    题目

    问题描述

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

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

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

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

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

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

    输入格式

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

    输出格式

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

    样例输入1

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

    样例输出1

    5

    样例输入2

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

    样例输出2

    1


    思路:

    这个其实怎么翻都无所谓的,从当前不一样的位置到下一个不一样的位置之间,怎么翻次数都是下标差。。。


    代码

    import java.util.*;
    import java.math.*;
    import java.util.regex.*;
    
    
    public class Main {
    
        final static int INF = 0x3f3f3f3f;
        final static int NUM = 100;
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            while(sc.hasNext()) {
                String s1=sc.next();
                String s2=sc.next();
                char[] ch1=s1.toCharArray();
                char[] ch2=s2.toCharArray();
                boolean[] bl = new boolean[s1.length()];
                int x=-1,y=-1;
                int ans=0;
                for(int i=0;i<ch1.length;i++) {
                    if(ch1[i]==ch2[i]) bl[i]=true;
                    else  {
                        bl[i]=false;
                        if(x==-1)x=i;
                        else y=i;
                    }
                    if(y!=-1) {
                        ans+=y-x;
                        x=-1;y=-1;
                    }
                }
    
                System.out.println(ans);
            }
    
        }
    }
    
  • 相关阅读:
    经典论文(转载)
    sublime编辑器Ctrl+E快捷键无效解决方法(转)
    Angular CLI 安装和使用(转发)
    tf.repeat() tensorflow
    colab使用
    记stanford-corenlp的使用
    gesim_word2vec训练词向量
    jupyter中不能用tensorflow
    Distributed Representations of Words and Phrases and their Compositionality论文阅读及实战
    A Neural Probabilistic Language Model_论文阅读及代码复现pytorch版
  • 原文地址:https://www.cnblogs.com/wygdove/p/4542096.html
Copyright © 2011-2022 走看看