首页 > 代码库 > Problem H: STL——括号匹配
Problem H: STL——括号匹配
Description
给出一堆括号,看其是否匹配,例如 ()、()()、(()) 这样的括号就匹配,
)(、)()) 而这样的括号就不匹配
Input
每一行代表一组测试样例,每组测试样例只包含‘(‘和‘)‘,样例长度不超过100个字符
Output
如果所有的括号都匹配,那么输出YES,否则输出NO
Sample Input
() )(
Sample Output
YES NO
HINT
使用STL的stack容易实现。
#include <iostream> #include <algorithm> #include <stack> #include <string> using namespace std; int main() { string s; stack<int> sta; while(cin>>s) { while(!sta.empty()) sta.pop(); int len=s.size(); for(int i=0;i<len;i++) { if(s[i]==‘(‘) sta.push(1); else { if((!sta.empty()) && (sta.top()==1)) sta.pop(); else sta.push(0); } } if(!sta.empty()) cout<<"NO"<<endl; else cout<<"YES"<<endl; } }
Problem H: STL——括号匹配
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。