您的位置:首页 > 其它

Problem I: STL——括号匹配

2017-04-03 21:37 429 查看

Description

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

Input

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

Output

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

Sample Input

())(

Sample Output

YESNO

HINT

使用STL的stack容易实现。

#include <iostream>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
int main()
{
string a;
while(cin>>a){
int len=a.size();stack<char> s;
for(int i=0;i<len;i++){
if(s.empty())
s.push(a[i]);
else if(a[i]==')'){
if(s.top()=='(')
s.pop();
}
else
s.push(a[i]);
}
if(s.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
while(!s.empty())
s.pop();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: