CF思维联系–CodeForces-217C C. Formurosa(这题鸽了)

2020-11-03 16:42:44 浏览数 (2)

ACM思维题训练集合 The Bytelandian Institute for Biological Research (BIBR) is investigating the properties of two species of bacteria, named simply 0 and 1. Even under a microscope, bacteria of those two species are very difficult to distinguish. In fact, the only thing the scientists possess that is able to differentiate between them is a plant called Formurosa.

If the scientists place a sample of colonies of bacteria on each on Formurosa’s leaves, it will activate a complicated nutrition process. During that process color of Formurosa changes to reflect the result of a — possibly very complicated — logical formula on the species of bacteria, involving constants and the operators | (OR), & (AND) and ^ (XOR). If it is 0, the plant will turn red, otherwise — it will turn blue.

For example, if the nutrition process of Formurosa is described by the formula: (((??)|?)&(1?)); then Formurosa has four leaves (the “?” signs denote the leaves). If we place 0, 1, 0, 0 on the respective leaves, the result of the nutrition process will be (((01)|0)&(10)) = 1, therefore the plant will turn blue.

The scientists have n colonies of bacteria. They do not know their types; the only thing they know for sure is that not all colonies are of the same type. They want to attempt to determine the bacteria’s species by repeated evaluations with Formurosa. During each evaluation they must place exactly one sample on every leaf of the plant. However, they may use multiple samples of one colony during a single evaluation; they can even cover the whole plant with bacteria from one colony!

Is it possible for them to always determine the species of each colony, no matter what they are (assuming they are not all the same)?

Input The first line of input contains a single integer n (2 ≤ n ≤ 106) — the number of colonies of bacteria.

The second line contains the formula describing the nutrition process of Formurosa. This line contains only characters «0», «1», «?», «|», «&», «^», «(», «)» and complies with the following grammar:

s → 0|1|?|(s|s)|(s&s)|(s^s) The formula consists of no more than 106 characters.

Output If it is always possible to determine the species of each colony, output “YES” (without quotes). Otherwise, output “NO” (without quotes).

Examples inputCopy 2 (?^?) outputCopy NO inputCopy 10 ? outputCopy YES inputCopy 2 ((?^?)&?) outputCopy YES

这题没看懂,做了三天,发现自己理解错了,鸽子了,抱歉,放个题解把。

代码语言:javascript复制
#include <stdio.h>
 
char str[2000000];
 
struct Ans
{
    int stat;
    int len;
};
 
int bxor (int a, int b)
{
    int re = 0;
 
    for (int i = 0; i < 16; i   )
        if (a & (1 << i))
            for (int j = 0; j < 16; j   )
                if (b & (1 << j))
                {
                    re |= (1 << (i ^ j));
                }
    return re;
}
 
int bor (int a, int b)
{
    int re = 0;
 
    for (int i = 0; i < 16; i   )
        if (a & (1 << i))
            for (int j = 0; j < 16; j   )
                if (b & (1 << j))
                {
                    re |= (1 << (i | j));
                }
    return re;
}
 
int band (int a, int b)
{
    int re = 0;
 
    for (int i = 0; i < 16; i   )
        if (a & (1 << i))
            for (int j = 0; j < 16; j   )
                if (b & (1 << j))
                {
                    re |= (1 << (i & j));
                }
    return re;
}
 
bool isok (Ans ans)
{
    if (ans.stat & (1 << (8   4   2)))
        return true;
    if (ans.stat & (1 << (8   4   1)))
        return true;
    if (ans.stat & (1 << (8   2   1)))
        return true;
    if (ans.stat & (1 << (4   2   1)))
        return true;
    if (ans.stat & (1 << (8   4)))
        return true;
    if (ans.stat & (1 << (8   2)))
        return true;
    if (ans.stat & (1 << (4   1)))
        return true;
    if (ans.stat & (1 << (2   1)))
        return true;
    if (ans.stat & (1 << (8)))
        return true;
    if (ans.stat & (1 << (4)))
        return true;
    if (ans.stat & (1 << (2)))
        return true;
    if (ans.stat & (1 << (1)))
        return true;
    return false;
}
 
Ans getans (int start)
{
    Ans ans;
 
    if (str[start] == '0')
    {
        ans.stat = (1 << 0);
        ans.len = 1;
        return ans;
    }
    if (str[start] == '1')
    {
        ans.stat = (1 << 15);
        ans.len = 1;
        return ans;
    }
    if (str[start] == '?')
    {
        ans.stat = (1 << (8 2))   (1 << (8 4));
        ans.len = 1;
        return ans;
    }
    Ans ans1, ans2;
    char op;
 
    start   ;
    ans1 = getans (start);
    start = start   ans1.len;
    op = str[start   ];
    ans2 = getans (start);
    
    ans.len = ans1.len   ans2.len   3;
    if (op == '^')
        ans.stat = bxor(ans1.stat, ans2.stat);
    if (op == '&')
        ans.stat = band(ans1.stat, ans2.stat);
    if (op == '|')
        ans.stat = bor(ans1.stat, ans2.stat);
 
    return ans;
}
 
int main ()
{
    int n;
    scanf ("%d%s", &n, str);
    printf ("%sn", isok(getans (0))? "YES": "NO");
    
    return 0;
}

0 人点赞