zoukankan      html  css  js  c++  java
  • Ural 1780 Gray Code 乱搞暴力

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780

    1780. Gray Code

    Time limit: 0.5 second
    Memory limit: 64 MB
    Denis, Vanya and Fedya gathered at their first team training. Fedya told them that he knew the algorithm for constructing aGray code.
    1. Create a 2-bit list: {0, 1}.
    2. Reflect this list and concatenate it with the original list: {0, 1, 1, 0}.
    3. Prefix old entries with 0, and prefix new entries with 1: {00, 01, 11, 10}.
    4. Repeat steps 2 and 3 until the length of all elements is equal to n.
    The number n is a length of a Gray code. For example, the code of length 3 is: {000, 001, 011, 010, 110, 111, 101, 100}.
    Denis ran the Fedya's algorithm and obtained a binary number x at position k (positions are numbered starting from zero). Vanya wrote down the numbers k and x in binary system. This story happened many years ago and now you hold the paper sheet with these numbers in your hands. Unfortunately, some digits are unreadable now. Could you determine the values of these digits using the readable digits?

    Input

    The first line contains a number k written in the binary system. Unreadable digits are denoted with symbol “?”. The second line contains a number x in the same format. The lengths of these numbers are equal and don't exceed 105. The numbers may contain leading zeroes.

    Output

    If there is a unique way to restore the numbers k and x, output them, replacing the symbols “?” with “0” or “1”. If there are multiple ways to restore them, output “Ambiguity”. If Denis or Vanya certainly made a mistake in these numbers, output “Impossible”.

    Samples

    inputoutput
    0?1
    0?0
    
    011
    010
    
    ?00
    ??0
    
    Ambiguity
    
    100
    100
    
    Impossible

    题意

    给你个格雷码和二进制,其中一些位置不确定,问你能不能相互转换,是否有多解。

    题解

    就乱搞,顺着推一次,然后倒着推一次,最后如若还有问号则多解,如果推的过程中有矛盾,则无解。至于为啥要推两次,大家脑补脑补就知道了。

    代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #define MAX_N 100005
    using namespace std;
    
    char k[MAX_N],x[MAX_N];
    
    bool deal(char &a,char &b,char &c) {
        if (a == '?') {
            if (b == '0') {
                if (c != '?')a = c;
                return true;
            }
            else if (b == '1') {
                if (c != '?')
                    a = '1' + '0' - c;
                return true;
            }
        }
        else if (a == '0') {
            if (b != c && b != '?' && c != '?')return false;
            if (b == '?')b = c;
            if (c == '?')c = b;
            return true;
        }
        else {
            if (b == c && b != '?')return false;
            if (b == '?' && c != '?')b = '1' + '0' - c;
            if (c == '?' && b != '?')c = '1' + '0' - b;
            return true;
        }
    }
    
    int main() {
        scanf("%s%s", k, x);
        bool now = 0;
        int n = strlen(k);
        for (int i = 0; i < n; i++) {
            char tmp = '0';
            bool flag = true;
            if (i == 0)flag = deal(tmp, k[i], x[i]);
            else flag = deal(k[i - 1], k[i], x[i]);
            if (!flag) {
                cout << "Impossible" << endl;
                return 0;
            }
        }
        for (int i = n - 1; i >= 0; i--) {
            char tmp = '0';
            bool flag = true;
            if (i == 0)flag = deal(tmp, k[i], x[i]);
            else flag = deal(k[i - 1], k[i], x[i]);
            if (!flag) {
                cout << "Impossible" << endl;
                return 0;
            }
        }
        bool flag = true;
        for (int i = 0; i < n; i++)
            if (x[i] == '?') {
                flag = false;
                break;
            }
        for (int i = 0; i < n; i++)
            if (k[i] == '?') {
                flag = false;
                break;
            }
        if (flag)
            printf("%s
    %s", k, x);
        else printf("Ambiguity
    ");
        return 0;
    }
  • 相关阅读:
    实训9.4.前端:url、href、src,link和@import
    实训9.2.作业1.写一个10次循环,每次得到一个随机数,放进一个集合中,如果这个数已经存在集合中则跳过,最后打印集合中的数字.
    实训9.3. SQL——STRUCTURED QUERY LANGUAGE(结构化查询语言 )
    实训9.2.类集,Collection接口
    实训9.2.IDEA ——java编程语言开发的集成环境(集成开发工具)
    实训9.2. JDK——java语言的软件开发工具包(JAVA的运行环境(JVM+Java系统类库)和JAVA工具) 【java开发的核心】
    从键盘输入数据
    error
    ubuntu 14.04, Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='
    用Python徒手写线性回归
  • 原文地址:https://www.cnblogs.com/HarryGuo2012/p/4728184.html
Copyright © 2011-2022 走看看