0
点赞
收藏
分享

微信扫一扫

Problem I: STL——括号匹配


Home

Web Board

ProblemSet

Standing

Status

Statistics


Problem I: STL——括号匹配


Time Limit: 1 Sec   Memory Limit: 128 MB

Submit: 3032  

Solved: 1855

[Submit][Status][Web Board]


Description



给出一堆括号,看其是否匹配,例如 ()、()()、(()) 这样的括号就匹配,



      )(、)()) 而这样的括号就不匹配



Input



每一行代表一组测试样例,每组测试样例只包含'('和')',样例长度不超过100个字符



Output



如果所有的括号都匹配,那么输出YES,否则输出NO



Sample Input



())(



Sample Output



YESNO



HINT



使用STL的stack容易实现。





Append Code


[ Submit][Status][Web Board]


한국어<  中文 فارسی English ไทย All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin:admin


#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
int main()
{
    stack <char> sta;
    char str[220];
    while( cin >> str )
    {
         int len = strlen(str);
        char *ch = str;
        if( str[0] == ')' || str[len - 1] == '(' || (len % 2) == 1)
        {
           cout << "NO" << endl;
           continue;
        }
        else
        {
           while(*ch != '\0')
           {
               if(sta.empty())
               {
                   char c = *ch;
                   sta.push(c);
               }
               else
               {
                   if(sta.top() == '(' && *ch == ')')
                       sta.pop();
                   else
                   {
                       char c = *ch;
                       sta.push(c);
                   }
               }
               ch++;
           }
        }
        if(sta.empty())
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
    }
}



举报

相关推荐

0 条评论