南阳OJ-2-括号配对问题---栈的应用

时间:2023-03-08 21:53:28

题目链接:

http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=2

题目大意:

有一行括号序列,请你检查这行括号是否配对。

思路:

直接用栈来模拟

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + ;
const int INF = << ;
int T, n, m;
int main()
{
cin >> T;
while(T--)
{
string s;
cin >> s;
bool ok = ;
stack<char>q;
for(int i = ; i < s.size(); i++)
{
if(s[i] == '[' || s[i] == '(')q.push(s[i]);
else
{
if(q.empty())
{
ok = ;
break;
}
char c = q.top();
//cout<<c<<endl;
q.pop();
if(s[i] == ']' && c != '[' || s[i] == ')' && c != '(')
{
ok = ;
break;
}
}
}
if(ok)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return ;
}