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;
    }
  • 相关阅读:
    最简明的JavaScript闭包解释
    REST vs SOAP
    MAC Objective-C 开发经典书籍推荐
    测试word版博客文章
    Sitecore CMS中删除项目
    Sitecore CMS中如何命名项目名称
    Sitecore CMS中查看标准字段
    Sitecore CMS中配置项目图标
    如何在Sitecore CMS中创建项目
    如何在Sitecore CMS中管理桌面快捷方式
  • 原文地址:https://www.cnblogs.com/HarryGuo2012/p/4728184.html
Copyright © 2011-2022 走看看