• HDU 5831 Rikka with Parenthesis II


    如果左括号数量和右括号数量不等,输出No

    进行一次匹配,看匹配完之后栈中还有多少元素:

    如果n=2,并且栈中无元素,说明是()的情况,输出No

    如果n=2,并且栈中有元素,说明是)(的情况,输出Yes

    如果n>2,并且栈中没有元素,或者有2个,或者有4个,输出Yes

    如果n>2,并且栈中元素个数大于4个,输出No

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    const double pi=acos(-1.0),eps=1e-8;
    void File()
    {
        freopen("D:\in.txt","r",stdin);
        freopen("D:\out.txt","w",stdout);
    }
    inline int read()
    {
        char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0;
        while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar();  }
        return x;
    }
    
    const int maxn=200010;
    stack<int >s;
    int T,n;
    char t[maxn];
    
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n); scanf("%s",t);
            while(!s.empty()) s.pop();
            int num1=0,num2=0;
            if(n%2==1) printf("No
    ");
            else
            {
                for(int i=0;t[i];i++)
                {
                    if(t[i]=='(') num1++; else num2++;
                    if(s.empty())
                    {
                        if(t[i]=='(') s.push(-1);
                        else s.push(1);
                    }
                    else
                    {
                        int num; if(t[i]=='(') num=-1; else num=1;
                        if(num==1&&s.top()==-1) s.pop();
                        else s.push(num);
                    }
                }
                if(num1!=num2) { printf("No
    "); continue; }
                if(n==2)
                {
                    if(s.size()==0) printf("No
    ");
                    else printf("Yes
    ");
                }
                else
                {
                    if(s.size()==0||s.size()==2||s.size()==4) printf("Yes
    ");
                    else printf("No
    ");
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    linux高编进程------system函数使用
    linux高编进程------用户权限
    linux高编进程------支持外部命令的shell实现(glob、strsep、fork)
    linux高编进程------exec函数族
    linux高编进程------进程分配
    1299. 将每个元素替换为右侧最大元素
    719. 找出第 k 小的距离对(二分)
    35. 搜索插入位置(二分)
    文件打不开,移到废纸篓
    119. 杨辉三角 II
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5765223.html
Copyright © 2020-2023  润新知