Ural 1780 Gray Code (暴力)

Ural 1780

题意:

输入一个二进制数与该二进制数的格雷码,但有一些位置不确定,写程序将其恢复,无法恢复输出Impossible,多解输出Ambiguity。

思路:

其实是个普通的乱搞题。。

Gray码的定义:Gi=Bi?1?Bi,G0=B0

(第i位格雷码等于第i位二进制码与第i-1位二进制码的异或值)

然后按照这个定义编码,正向反向各自编一遍,出现矛盾输出Impossible,最后还含有’?’则有多解。

代码:

/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;

bool check( char &k1 , char &k0 , char &x1 ){
        //cout << k1 << k0 << x1 << endl;
    if( k0 == ‘?‘ ) {
        if( k1 == ‘0‘ ) {
            if( x1 != ‘?‘ )
                k0 = x1 ;
            return true ;
        }
        else if( k1 == ‘1‘ ) {
            if( x1 != ‘?‘ )
                k0 = ‘1‘ + ‘0‘ - x1 ;
            return true ;
        }
    }
    else if( k0 == ‘0‘ ){
        if( k1 != x1 && k1 != ‘?‘ && x1 != ‘?‘ )
            return false ;
        if( k1 == ‘?‘ )
            k1 = x1 ;
        if( x1 == ‘?‘ )
            x1 = k1 ;
        return true ;
    }
    else {
        if( k1 == x1 && k1 != ‘?‘ )
            return false ;
        if( k1 == ‘?‘ && x1 != ‘?‘ )
            k1 = ‘1‘ + ‘0‘ - x1 ;
        if( x1 == ‘?‘ && k1 != ‘?‘ )
            x1 = ‘1‘ + ‘0‘ - k1 ;
        return true ;
    }
}
int main(){
  //freopen("input.txt","r",stdin);
    string x , k ;
    while( cin >> k >> x ){
        k = ‘0‘ + k ;
        int len = x.length() ;
        int flag = 0 ;
        for( int i = 0 ; i < len ; i++ ){
            if( !check( k[i+1] , k[i] , x[i]) ) {
                cout << "Impossible" << endl;
                flag = 1 ;
                break ;
            }
        }
        if( flag ) continue ;
        for( int i = len - 1 ; i >= 0 ; i-- ){
            if( !check( k[i+1] , k[i] , x[i] ) ) {
                cout << "Impossible" << endl ;
                flag = 1 ;
                break ;
            }
        }
        k.erase(0,1);
        if( flag ) continue ;
        if( k.find( "?" ) != string::npos || x.find( "?" ) != string::npos ){
            cout << "Ambiguity" << endl;
            continue ;
        }
        cout << k << endl ;
        cout << x << endl ;
        k.clear() ;
        x.clear() ;
    }
    return 0;
}

版权声明:博主表示授权一切转载啦:)

时间: 2024-10-12 09:11:23

Ural 1780 Gray Code (暴力)的相关文章

Ural 1780 Gray Code 乱搞暴力

原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMemory 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. Cre

leetcode笔记:Gray Code(2016腾讯软件开发笔试题)

一.题目描述 The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin wit

LeetCode:Gray Code

1.题目名称 Gray Code(格雷码) 2.题目地址 https://leetcode.com/problems/gray-code/ 3.题目内容 英文: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the

LeetCode89 Gray Code

题目: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0

hdu 5375 Gray code(DP)

hdu 5375 Gray code Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed

LeetCode --- 89. Gray Code

题目链接:Gray Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must b

【leetcode】Gray Code

Gray Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin

LeetCode: Gray Code [089]

[题目] The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with

leetcode 89 Gray Code ----- java

The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Fo