zoukankan      html  css  js  c++  java
  • URAL 1780 G

    G - Gray Code
    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87643#problem/G

    Description

    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 10 5. 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”.

    Sample Input

    0?1
    0?0

    Sample Output

    011
    010

    HINT

    题意

    题目给了一种序列的构造方式

    然后给你一个k,判断第k个数是不是说给的x

    题解

    找规律题,注意翻转= =

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <stack>
    #include <map>
    #include <set>
    #include <queue>
    #include <iomanip>
    #include <string>
    #include <ctime>
    #include <list>
    #include <bitset>
    typedef unsigned char byte;
    #define pb push_back
    #define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
    #define local freopen("in.txt","r",stdin)
    #define pi acos(-1)
    
    const char * fail = "Impossible";
    const char * more = "Ambiguity";
    using namespace std;
    const int maxn = 1e5 + 500;
    int dp[maxn][2];
    char s1[maxn] , s2[maxn];
    
    int main(int argc,char *argv[])
    {
        scanf("%s%s",s1+1,s2+1);
        int Length = strlen(s1+1);
        s1[0] = '0';
        for(int i = Length ; i > 0 ; -- i)
        {
            if (s2[i] == '0')
            {
                //同号
                if (s1[i] == '0' && s1[i-1] == '0') continue;
                if (s1[i] == '0' && s1[i-1] == '1')
                {
                    printf("%s
    ",fail);
                    return 0;
                }
                if(s1[i] == '0' && s1[i-1] == '?')
                {
                    s1[i-1] = '0';
                    continue;
                }
                if (s1[i] == '1' && s1[i-1] == '0')
                {
                    printf("%s
    ",fail);
                    return 0;
                }
                if (s1[i] == '1' && s1[i-1] == '1') continue;
                if (s1[i] == '1' && s1[i-1] == '?')
                {
                    s1[i-1] = '1';
                    continue;
                }
                if (s1[i] == '?' && s1[i-1] == '0')
                {
                    s1[i] = '0';
                    continue;
                }
                if (s1[i] == '?' && s1[i-1] == '1')
                {
                    s1[i] = '1';
                    continue;
                }
                if (s1[i] == '?' && s1[i-1] == '?') continue;
            }
            if (s2[i] == '1')
            {
                //异号
                if (s1[i] == '0' && s1[i-1] == '1') continue;
                if (s1[i] == '0' && s1[i-1] == '0')
                {
                    printf("%s
    ",fail);
                    return 0;
                }
                if(s1[i] == '0' && s1[i-1] == '?')
                {
                    s1[i-1] = '1';
                    continue;
                }
                if (s1[i] == '1' && s1[i-1] == '0') continue;
                if (s1[i] == '1' && s1[i-1] == '1')
                {
                    printf("%s
    ",fail);
                    return 0;
                }
                if(s1[i] == '1' && s1[i-1] == '?')
                {
                    s1[i-1] = '0';
                    continue;
                }
                if(s1[i] == '?' && s1[i-1] == '0')
                {
                    s1[i] = '1';
                    continue;
                }
                if(s1[i] == '?' && s1[i-1] == '1')
                {
                    s1[i] = '0';
                    continue;
                }
                if(s1[i] == '?' && s1[i-1] == '?') continue;
            }
        }
        for(int i=1;i<=Length;i++) 
            if(s1[i]=='?') 
            {
                if(s2[i]!='?')
                {
                    if(s2[i]=='0') s1[i]=s1[i-1];else s1[i]='0'+1-(s1[i-1]-'0');
                    continue;
                }
                printf("%s
    ",more);
                return 0;
            }
        for(int i=0;i<Length;i++) 
            if(s1[i]!=s1[i+1])
            {
                s2[i+1]='1';
            }
            else s2[i+1]='0';
        printf("%s
    %s
    ",s1+1,s2+1);
        return 0;
    }
  • 相关阅读:
    Ubuntu 出现 apt-get问题的解决方法
    Conversion to Dalvik format failed: Unable to execute dex: null
    java中判断是否为中文
    解决:Unable to connect to repository https://dl-ssl.google.com/android/eclipse/site.xml
    Android 获取本机WIFI及3G网络IP
    xp重装系统后恢复Linux启动
    Android中空格及换行
    ubuntu12.10设置thunderbird开机自启动
    web安全及防护
    回归基础: JavaScript 变量提升
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4728618.html
Copyright © 2011-2022 走看看