Problem I: STL——括号匹配
2017-04-03 21:37
429 查看
Description
给出一堆括号,看其是否匹配,例如 ()、()()、(()) 这样的括号就匹配,)(、)()) 而这样的括号就不匹配
Input
每一行代表一组测试样例,每组测试样例只包含'('和')',样例长度不超过100个字符Output
如果所有的括号都匹配,那么输出YES,否则输出NOSample Input
())(Sample Output
YESNOHINT
使用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();
}
}
相关文章推荐
- Problem E: STL——括号匹配
- Problem D: STL——括号匹配
- Problem : STL——括号匹配
- Problem I: STL——括号匹配
- Problem E: STL——括号匹配
- 括号匹配--STL--栈的使用--南阳OJ
- c++STL的stack的应用之括号匹配
- STL——括号匹配
- stl——括号匹配
- nyoj2括号匹配 STL
- 栈匹配括号(STL实现)
- STL实现括号匹配
- 7-10 括号匹配(25 分) 【STL】
- 数据结构栈之括号匹配(STL实现&&数组栈实现)
- C++抽象编程——STL(2)——括号匹配与口袋计算器
- 详解括号匹配问题(STL stack)
- CSU Problem 1809 Parenthesis(括号匹配,线段树,前缀和)——湖南省第十二届大学生计算机程序设计竞赛
- STL版 括号匹配(感觉不如之前自己用数组模拟的跑的快)
- Problem C: 括号匹配(栈和队列)
- CSUST选拔赛题解之-Problem G: 括号匹配