#include <iostream>
#include <stack>
using namespace std;
typedef stack STACK;
int main(){
char c;
string str;
while(cin >> str) {
STACK st;
string::iterator s_it = str.begin();
int flag =0;
while(s_it != str.end()) {
if(*s_it == '(')
st.push(*s_it);
else {
if (!st.empty()) st.pop();
else {
cout << "NO"<<endl; flag =1; break;
}
}
s_it++;
}
if(flag !=1 && st.empty())
cout<<"YES" << endl;
else if(flag !=1 && !st.empty())
cout << "NO" <<endl;
}
return 0;
}
本文介绍了一个使用C++和标准模板库(STL)中的stack数据结构实现的括号匹配程序。该程序能够读取一行包含括号的字符串,并判断这行字符串中的括号是否正确配对。
2106

被折叠的 条评论
为什么被折叠?



