zoukankan      html  css  js  c++  java
  • 数据结构实验之栈四:括号匹配 分类: 栈和队列 2015-06-18 17:06 13人阅读 评论(0) 收藏

    数据结构实验之栈四:括号匹配

    TimeLimit: 1000ms Memory limit: 65536K 有疑问?点这里^_^

    题目描述

    给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的() ,[ ],{ }是否匹配。

    输入

    输入数据有多组,处理到文件结束。

    输出

    如果匹配就输出“yes”,不匹配输出“no”

    示例输入

    sin(20+10)

    {[}]

    示例输出

    yes

    no

    #include <map>
    #include <set>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <vector>
    #include <string>
    #include <climits>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #define RR freopen("input.txt","r",stdin)
    #define WW freopen("output.txt","w",stdout)
    #define INF 0x3f3f3f3f
    using namespace std;
    int trans(char s)
    {
        if(s=='[')
            return 1;
        if(s==']')
            return 6;
        if(s=='{')
            return 2;
        if(s=='}')
            return 5;
        if(s=='(')
            return 3;
        if(s==')')
            return 4;
        return 0;
    }
    int main()
    {
        char str[100];
        int s[110];
        while(gets(str))
        {
            int len=strlen(str);
            int ans,top=0;
            bool flag=true;
            for(int i=0; i<len; i++)
            {
                ans=trans(str[i]);
                if(ans)
                {
                    if(ans<=3)
                    {
                        s[++top]=ans;
                    }
                    else
                    {
                        if(top&&s[top]+ans==7)
                        {
                            top--;
                        }
                        else
                        {
                            flag=false;
                            break;
                        }
                    }
                }
            }
            if(top||!flag)
            {
                printf("no
    ");
            }
            else
            {
                printf("yes
    ");
            }
        }
        return 0;
    }
    <span style="font-family:华文楷体, serif;">/*
    STL —— stack
    */
    </span>#include <map>
    #include <set>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <vector>
    #include <string>
    #include <climits>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #define RR freopen("input.txt","r",stdin)
    #define WW freopen("output.txt","w",stdout)
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    int trans(char s)
    {
        if(s=='[')
            return 1;
        if(s==']')
            return 6;
        if(s=='{')
            return 2;
        if(s=='}')
            return 5;
        if(s=='(')
            return 3;
        if(s==')')
            return 4;
        return 0;
    }
    int main()
    {
        char str[100];
        while(gets(str))
        {
            int len=strlen(str);
            int ans;
            bool flag=true;
            stack<int>s;
            for(int i=0; i<len; i++)
            {
                ans=trans(str[i]);
                if(ans)
                {
                    if(ans<=3)
                    {
                        s.push(ans);
                    }
                    else
                    {
                        if(!s.empty()&&s.top()+ans==7)
                        {
                            s.pop();
                        }
                        else
                        {
                            flag=false;
                            break;
                        }
                    }
                }
            }
            if(!s.empty()||!flag)
            {
                printf("no
    ");
            }
            else
            {
                printf("yes
    ");
            }
        }
        return 0;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    hdu4578 (多标记线段树)
    hdu4757 (可持久化字典树+LCA)
    CF940F Machine Learning (带修改莫队)
    csps模拟测试7576一句话题解
    csps模拟测试74梦境,玩具,飘雪圣域题解
    csps模拟测试7273简单的操作小P的2048小P的单调数列小P的生成树
    csps模拟测试707172部分题解myc
    莫队算法学习
    csps模拟69chess,array,70木板,打扫卫生题解
    csps模拟68d,e,f题解
  • 原文地址:https://www.cnblogs.com/juechen/p/4722019.html
Copyright © 2011-2022 走看看