括号匹配

发布时间:2019年09月10日 阅读:314 次

c++括号匹配

http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2134.html

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int  maxn  =1e6+7;
int a[200][200];
int main()
{

   char s[60];
   while(gets(s))
   {
       int len = strlen(s);
       char Stack[99999];
       int top =0,f = 0;
       for(int i=0;i<len;i++)
       {
            if(s[i]=='('||s[i]=='['||s[i]=='{')
                {
                    Stack[top++] = s[i];
                }
                else
                {
                    if(s[i]==')')
                    {
                        if(top ==0||Stack[top-1]!='(')
                        {
                            f =1;
                            break;
                        }
                        else if(Stack[top-1]=='(')
                        {
                            top--;
                        }
                    }
                      if(s[i]==']')
                    {
                        if(top ==0||Stack[top-1]!='[')
                        {
                            f =1;
                            break;
                        }
                        else if(Stack[top-1]=='[')
                        {
                            top--;
                        }
                    }
                      if(s[i]=='}')
                    {
                        if(top ==0||Stack[top-1]!='{')
                        {
                            f =1;
                            break;
                        }
                        else if(Stack[top-1]=='{')
                        {
                            top--;
                        }
                    }
                }
       }
       if(!f&top==0)
        printf("yes\n");
       else
        printf("no\n");
   }
    return 0;
}



/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 0ms
Take Memory: 196KB
Submit time: 2017-10-14 21:48:36
****************************************************/


Tag:
相关文章

发表评论: