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 a Gray 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\n%s", k, x);
    else printf("Ambiguity\n");
    return 0;
}

 

 

转载于:https://www.cnblogs.com/HarryGuo2012/p/4728184.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值