10 条题解

  • -1
    @ 2023-2-23 21:43:14
    /*********************************
    备注:
    *********************************/
    #include<map>
    #include<list>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<deque>
    #include<math.h>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<cstring>
    #include<fstream>
    #include<stdio.h>
    #include<sstream>
    #include<iomanip>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #include<bits/stdc++.h>
    using namespace std;
    #define in int
    #define ch char
    #define lo long
    #define fl float
    #define sh short
    #define db double
    #define ll long long
    #define ld long double
    #define lli long long int
    const int N =1e6+1;
    const int INF =0x3f3f3f3f;
    char str[N];
    int c1[N],c2[N],l1[N],l2[N],cnt1,cnt2;
    int dfs(int l,int r)
    {
        if(c1[r]>=l)
    	{
            int ans=dfs(l,c1[r]-1);
            if (ans==1)
    		{
                ++cnt1;
                return 1;
            }
            return (ans|dfs(c1[r]+1,r));
        }
        if(c2[r]>=l)
    	{
            int ans=dfs(l,c2[r]-1);
            if (ans==0)
    		{
                ++cnt2;
                return 0;
            }
            return (ans&dfs(c2[r]+1,r));
        }
        if (str[l]=='('&&str[r]==')')
    	{
            return dfs(l+1,r-1);
        }
        return str[l]-'0';
    }
    int main(int argc, const char * argv[])
    {
        scanf("%s",str + 1);
        int len=strlen(str+1);
        int x=0; 
        for(int i=1;i<=len;i++)
    	{
            if(str[i]=='(')
    		{
                x++;
            }
    		else if(str[i]==')')
    		{
                --x;
            }
    		else if(str[i]=='|')
    		{
                l1[x]=i;
            }else if(str[i]=='&')
    		{
                l2[x]=i;
            }
            c1[i]=l1[x];
            c2[i]=l2[x];
        }
        int ans=dfs(1,len);
        cout<<ans<<endl<<cnt2<<" "<<cnt1;
        return 0;
    }
    

    「CSP-J 2022」逻辑表达式(expr)

    信息

    ID
    2918
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    156
    已通过
    48
    上传者