1 solutions

  • 0
    @ 2024-6-18 14:37:44

    模拟(20pts)

    • 分别枚举长度为3和5的串的情况,
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        string s;
        cin>>s;
        if(s=="1&1")
        {
            cout<<"1"<<endl;
            cout<<"0 0"<<endl;
        }
        else if(s=="1|1")
        {
            cout<<"1"<<endl;
            cout<<"0 1"<<endl;
        }
        else if(s=="0&1")
        {
            cout<<"0"<<endl;
            cout<<"1 0"<<endl;
        }
        else if(s=="1|0")
        {
            cout<<"1"<<endl;
            cout<<"0 1"<<endl;
        }
        else if(s=="0|1")
        {
            cout<<"1"<<endl;
            cout<<"0 0"<<endl;
        }
        else if(s=="0&0")
        {
            cout<<"0"<<endl;
            cout<<"1 0"<<endl;
        }
        else if(s=="1&1")
        {
            cout<<"1"<<endl;
            cout<<"0 0"<<endl;
        }
        else if(s=="0|0")
        {
            cout<<"0"<<endl;
            cout<<"0 1"<<endl;
        }
        return 0;
    }
    

    表达式树(100pts)

    • 类似之前一个题,标记当前节点是否对结果有影响。
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e6+10;
    int l[N],r[N],idx;
    char w[N];
    stack<int> nums;
    stack<char> ops;
    int cnt1,cnt2;
    void build()
    {
        int b=nums.top();nums.pop();
        int a=nums.top();nums.pop();
        w[++idx]=ops.top();ops.pop();
        l[idx]=a,r[idx]=b;
        nums.push(idx);
    }
    int dfs(int u)
    {
       if(w[u]=='0') return 0;
       if(w[u]=='1') return 1;
       if(w[u]=='|') //或运算
       {
           if(dfs(l[u])==1) //左儿子节点是1,则说明当前算是短路
           {
               cnt2++;
               return 1;
           }
           else return dfs(r[u]);
       }
       else //与运算
       {
           if(dfs(l[u])==0) //左儿子为0
           {
               cnt1++;
               return 0;
           }
           else return dfs(r[u]);
       }
    }
    int main()
    {
        map<char,int> pr={{'|',1},{'&',2}}; //符号优先级
        string s;
        cin>>s;
        int n=s.size();
        for(int i=0;i<n;i++)
        {
            if(s[i]=='(') //左括号
            {
                ops.push(s[i]);
            }
            else if(s[i]==')') //右括号
            {
                while(ops.size()&&ops.top()!='(') build(); //先计算括号里面的
                ops.pop();
            }
            else if(isdigit(s[i])) //叶子节点
            {
                w[++idx]=s[i];
                nums.push(idx);
            }
            else
            {
                while(ops.size()&&pr[ops.top()]>=pr[s[i]]) //计算优先级大于等于当前元素的
                {
                    build();
                }
                ops.push(s[i]);
            }
        }
        while(ops.size()) build(); //建立剩余的表达式树
        int ans=dfs(idx); //搜索得到答案
        cout<<ans<<endl;
        cout<<cnt1<<" "<<cnt2<<endl;
        return 0;
    }
    
    • 1

    Information

    ID
    1077
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    # Submissions
    1
    Accepted
    1
    Uploaded By