Question address: http://acm.timus.ru/problem.aspx? Space = 1 & num = 1780
Question meaning:
Give you a gray code binary and Gray Code itself
But some of them are? Instead, ask if this result is unique.
Output is unique. output is ambiguity if it is not unique. Impossible is output if it does not exist.
Solution:
This question is not difficult. The only problem is to understand gray code.
The conversion method of gray code is as follows:
Binary Code-> gray code (encoding): Starting from the rightmost one, each digit is in turn different from the one on the left or (XOR ), as the value corresponding to the gray code, the leftmost digit remains unchanged (equivalent to 0 on the left );
Gray code-> binary code (Decoding): Each decoded value is different from the decoded value of the second digit on the left or, the decoded value (the leftmost one remains unchanged ).
In addition, the first digit on the left of the gray code must be the same as the first digit on the left of the binary code.
After learning about the above, you can do it.
It will not stop decoding, encoding until it can no longer be encoded or decoded.
Then make a judgment
BelowCode:
# Include <cstdio> # include <cstring> # include <cstdlib> using namespace STD; const int maxn = 100000 + 10; int A [maxn]; int B [maxn]; char S1 [maxn]; char S2 [maxn]; int N; int main () {While (~ Scanf ("% S % s", S1, S2) {n = strlen (S1); For (INT I = 0; I <n; I ++) {If (S1 [I] = '? ') A [I] =-1; else a [I] = S1 [I]-'0'; If (s2 [I] = '? ') B [I] =-1; else B [I] = S2 [I]-'0';} bool F = true; bool F2 = true; while (F & F2) // exit {f = false if new or conflicting versions are not released; // first query for (INT I = n-1; I> = 1; I --) {if (a [I]! =-1 & A [I-1]! =-1) {int TMP = A [I] ^ A [I-1]; If (B [I] =-1) {B [I] = TMP; F = true; // printf ("B % d \ n", I);} else {If (B [I]! = TMP) // The rule does not match. You can directly jump to {F2 = false; break ;}} if (a [I]! =-1 & B [I]! =-1) {int TMP = A [I] ^ B [I]; if (a [I-1] =-1) {A [I-1] = TMP; // printf ("A % d \ n", I-1); F = true;} else {if (a [I-1]! = TMP) {F2 = false; break ;}} if (B [I]! =-1 & A [I-1]! =-1) {int TMP = B [I] ^ A [I-1]; if (a [I] =-1) {f = true; A [I] = TMP;} else {if (a [I]! = TMP) {F2 = false; break ;}}// from the leftmost forget to right query if (F2) for (INT I = 1; I <N; I ++) {If (B [I]! =-1 & A [I-1]! =-1) {int TMP = B [I] ^ A [I-1]; if (a [I] =-1) {f = true; A [I] = TMP;} else {if (a [I]! = TMP) {F2 = false; break ;}} if (a [I]! =-1 & A [I-1]! =-1) {int TMP = A [I] ^ A [I-1]; If (B [I] =-1) {B [I] = TMP; F = true; // printf ("B % d \ n", I);} else {If (B [I]! = TMP) // The rule does not match. You can directly jump to {F2 = false; break ;}} if (a [I]! =-1 & B [I]! =-1) {int TMP = A [I] ^ B [I]; if (a [I-1] =-1) {A [I-1] = TMP; // printf ("A % d \ n", I-1); F = true;} else {if (a [I-1]! = TMP) {F2 = false; break ;}}} if (! F2) break; // if (a [0]! =-1 & B [0] =-1) {B [0] = A [0]; F = true ;} if (A [0] =-1 & B [0]! =-1) {A [0] = B [0]; F = true ;}} if (! F2 | A [0]! = B [0]) // when there is a conflict, the leftmost part is the specially judged {printf ("impossible \ n");} else {bool F3 = true; for (INT I = 0; I <n; I ++) {if (a [I] =-1 | B [I] =-1) // still not available, it is not unique {printf ("ambiguity \ n"); F3 = false; break ;}} if (F3) {for (INT I = 0; I <n; I ++) printf ("% d", a [I]); printf ("\ n "); for (INT I = 0; I <n; I ++) printf ("% d", B [I]); printf ("\ n ");}}} return 0 ;}