Algorithm
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101.
The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.
An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
????
00
01
10
11
1010
10
1?1
01
11
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.
In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
char s[N];
int main() {
scanf("%s", s);
int len = strlen(s);
int zc = 0, oc = 0, qc, zcn, ocn, g = len - 2;
for(int i = 0; i < len; ++i)
zc += s[i] == '0', oc += s[i] == '1';
qc = len - (zc + oc);
// 00
zcn = g / 2 - zc + 2; // petya
ocn = ceil(g / 2.0) - oc; // masha
if(zcn < 0) zcn = 0;
if(ocn < 0) ocn = 0;
if(zcn <= 0 || zcn + ocn == qc)
puts("00");
// 01
zcn = g / 2 - zc + 1; // petya
ocn = ceil(g / 2.0) - oc + 1; // masha
if(zcn < 0) zcn = 0;
if(ocn < 0) ocn = 0;
if((zcn <= 0 && ocn <= 0 || zcn + ocn == qc) && s[len - 1] == '1')
puts("01");
else if(s[len - 1] == '?') {
zcn = g / 2 - zc + 1; // petya
ocn = ceil(g / 2.0) - oc; // masha
if(zcn < 0) zcn = 0;
if(ocn < 0) ocn = 0;
if(zcn <= 0 && ocn <= 0 || zcn + ocn == qc - 1)
puts("01");
}
// 10
zcn = g / 2 - zc + 1; // petya
ocn = ceil(g / 2.0) - oc + 1; // masha
if(zcn < 0) zcn = 0;
if(ocn < 0) ocn = 0;
if((zcn <= 0 && ocn <= 0 || zcn + ocn == qc) && s[len - 1] == '0')
puts("10");
else if(s[len - 1] == '?') {
zcn = g / 2 - zc; // petya
ocn = ceil(g / 2.0) - oc + 1; // masha
if(zcn < 0) zcn = 0;
if(ocn < 0) ocn = 0;
if(zcn <= 0 && ocn <= 0 || zcn + ocn == qc - 1)
puts("10");
}
// 11
zcn = g / 2 - zc; // petya
ocn = ceil(g / 2.0) - oc + 2; // masha
if(zcn < 0) zcn = 0;
if(ocn < 0) ocn = 0;
if(ocn <= 0 || zcn + ocn == qc)
puts("11">;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
01
10
11
Demonstration
Codeforcess Solution Zero-One ,E. Zero-One C,C++, Java, Js and Python ,Zero-One,Codeforcess Solution