Codeforces 135C Zero-One(博弈)

C. Zero-One
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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).

Examples
input
Copy
????
output
Copy
00
01
10
11
input
Copy
1010
output
Copy
10
input
Copy
1?1
output
Copy
01
11
Note

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.


【题目链接】点击有惊喜

【题意】有一个长度为n的字符串,字符串由0,1,?组成,?即可能是0也可能是1,有A,B两个选手一次取字符串,每次只能够取一个(如果取完后字符串分成两部分则再拼接起来),A的目的是使的01字符串对应的十进制最小,B的目的是使的01字符串对应的十进制最大,最后剩01串长度为2时截止。

【思路】设one为字符串中1的个数,zero为字符串中0的个数。通过一个例子可以发现以下规律,当字符串中不存在?时,zero>one 最后输出的为"00",当one>zero+1时,输出"11,当zero==one || one == zero+1 的时候,如果字符串的最后一个字符为0,则输出 "10"。

对于当字符串中存在?时,思考的方式也一样。

【代码如下】

#include <bits/stdc++.h>
using namespace std;

int main(){
    string str;
    cin >> str;
    int n = str.length();
    int zero=0,one=0,other=0;
    for(int i = 0; i < n; i ++) if(str[i]=='0') zero++; else if(str[i]=='1') one++; else other++;
    int p = n/2-1, q = (n+1)/2-1;
    if(one <= q) puts("00");
    if(str[n-1] != '0' && zero-1 <= p && one+(str[n-1]=='?')-1 <= q) puts("01");
    if(str[n-1] != '1' && zero+(str[n-1]=='?')-1<=p && one-1<=q) puts("10");
    if(zero <= p) puts("11");
    return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/i1020/article/details/79960827
个人分类: CodeForces 博弈 贪心
上一篇Anticube
下一篇Codeforces 150A Win or Freeze (博弈 &amp; 数论)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭