zoukankan      html  css  js  c++  java
  • 字符串匹配问题——带有优先级的括号匹配问题

    题目描述

    字符串中只含有括号 (),[],<>,{},判断输入的字符串中括号是否匹配。如果括号有互相包 含的形式,从内到外必须是<>,(),[],{},例如。输入: [()] 输出:YES,而输入([]),([)]都应该 输出 NO。

    输入

    第一行为一个整数 n,表示以下有多少个由括好组成的字符串。接下来的 n 行, 每行都是一个由括号组成的长度不超过 255 的字符串。

    输出

    有 n 行(n≤20),每行都是 YES 或 NO。

    样例输入

    5
    {}{}<><>()()[][]
    {{}}{{}}<<>><<>>(())(())[[]][[]]
    {{}}{{}}<<>><<>>(())(())[[]][[]]
    {<>}{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]
    ><}{{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]
    

    样例输出

    YES
    YES
    YES
    YES
    NO
    
    #include <bits/stdc++.h>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <vector>
    using namespace std;
    #define wuyt main
    typedef long long ll;
    template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
    template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
    ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
    if(c == '-')Nig = -1,c = getchar();
    while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
    return Nig*x;}
    #define read read()
    const ll inf = 1e15;
    const int maxn = 2e5 + 7;
    const int mod = 1e9 + 7;
    #define start int wuyt()
    #define end return 0
    /**int dfs(int n,int k)
    {
        if((n==0)||(k==0)||(n<k))
    		return 0;
        if(k==1||n==k)
    		return 1;///only1
        return dfs(n-k,k)+dfs(n-1,k-1);
    }**/
    char ss[maxn];
    int num[maxn];
    start{
        ll n=read;
        while(n--)
        {
            cin>>ss;
            for(int i=0;i<strlen(ss);i++)
            {
                if(ss[i]=='<') num[i]=1;
                else if(ss[i]=='>') num[i]=2;
                else if(ss[i]=='(') num[i]=3;
                else if(ss[i]==')') num[i]=4;
                else if(ss[i]=='[') num[i]=5;
                else if(ss[i]==']') num[i]=6;
                else if(ss[i]=='{') num[i]=7;
                else if(ss[i]=='}') num[i]=8;
             }
             stack<int> s;
             bool flag=true;
             for(int i=0;i<strlen(ss);i++)
             {
                 if(s.empty()) s.push(num[i]);
                 else{
                    if(num[i]%2==1){
                        if(num[i]<=s.top()) s.push(num[i]);
                        else {
                            flag=false;
                            break;
                        }
                    }
                    else if(num[i]-1==s.top())
                        s.pop();
                 }
             }
             if(s.empty()&&flag) printf("YES
    ");
             else printf("NO
    ");
        }
    	end;
    }
    
    

    前面哪一种是错误的方法,没有看到需要考虑类似优先级这样的问题

  • 相关阅读:
    强化学习(David Silver)2:MDP
    【论文阅读-DL】《Understanding Black-box Predictions via Influence Functions》阅读
    强化学习(David Silver)1:简介
    【论文阅读-DL】《Understanding Black-box Predictions via Influence Functions》阅读
    生成模型和判别模型
    第6章:概率图模型
    基础算法问题
    【论文阅读-CTR】<<Optimized Cost per Click in Taobao Display Advertising>>阅读
    【论文阅读-对话系统】<<Neural Responding Machine for Short-Text Conversation>>阅读
    【读书笔记】——终极算法
  • 原文地址:https://www.cnblogs.com/PushyTao/p/13144167.html
Copyright © 2011-2022 走看看