Problem E: STL——括号匹配
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 5416 Solved: 3489
[Submit][Status]
Description
给出一堆括号,看其是否匹配,例如 ()、()()、(()) 这样的括号就匹配,
)(、)()) 而这样的括号就不匹配
Input
每一行代表一组测试样例,每组测试样例只包含'('和')',样例长度不超过100个字符
Output
如果所有的括号都匹配,那么输出YES,否则输出NO
Sample Input
() )(
Sample Output
YES NO
HINT
使用STL的stack容易实现。
Append Code
#include<bits/stdc++.h>
using namespace std;
int main() {
string a;
while (cin>>a)
{
int jishu=0;
int flag=0;
for(int i=0;i<a.length();i++)
{
if(a[i]=='(')
{
jishu++;
}
else
{
jishu--;
if(jishu<0)
{
cout<<"NO"<<endl;
flag=1;
break;
}
}
}
if(jishu==0&&flag==0)
{
cout<<"YES"<<endl;
}
else if (flag==1)
{
/* code */
}
else
{
cout<<"NO"<<endl;
}
}
}