括号的匹配

来源:互联网 发布:淘宝双十一2017 编辑:程序博客网 时间:2024/06/11 18:55
//题目介绍:比如输入一行只包含“()[]{}"的字符串,请判断形如"([{}])”是否正确
#include <stdio.h>
#include <stack>
#include <string.h>
using namespace std;
 int main()//这代码都比较好懂,下一篇小猫钓鱼会多一点
 {
     stack<char>q;
     char s[100];
     int n,i,j,l;
     scanf("%s",s);
     l=strlen(s);
     q.push(s[0]);
     for (i=1;i<l;i++)
     {

         if (q.empty())
         {
             q.push(s[i]);
             continue;

         }
         else
             if (q.top()=='(')
             {
                 if (s[i]==')')
                 {
                 q.pop();
                 continue;
                 }
                  else
                  {
                 q.push(s[i]);
                continue;
                  }
             }
             if (q.top()=='[')
             {
                if (s[i]==']')
                 {
                q.pop();
                continue;
                 }
                  else
                  {
                q.push(s[i]);
                continue;
                  }
             }
             if (q.top()=='{')
                {
                    if (s[i]=='}')
                {
                q.pop();
                continue;
                }
                    else
                    {q.push(s[i]);
                    continue;
                    }
             }

     }
     if (q.empty())
        printf("Yes\n");
     else
        printf("No\n");

}

0 0
原创粉丝点击