题目
数据
3
({[()]})
{[()()][()]}[()]{[()]}
{()}
Yes
Yes
No
思路
用栈秒了。。判断yes之前记得判断栈是不是空的
代码
#include <iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int alpha[130];
int main()
{
alpha['('] = 1;alpha[')'] = 2;
alpha['['] = 3;alpha[']'] = 4;;
alpha['{'] = 5; alpha['}'] = 6;
int t;
cin >> t;
for(int z = 0; z < t; z++)
{
stack<char> s;
string str;
cin >> str;
int len = str.length();
int flag = true;
for(int i = 0; i < len; i++)
{
if(s.empty())
{
s.push(str[i]);
continue;
}
switch(str[i])
{
case '(':
if(s.top() == '[')
{
s.push(str[i]);
}
else{
flag = false;
goto FLAG;
}
break;
case')':
if(s.top() == '(')
{
s.pop();
}
else{
flag = false;
goto FLAG;
}
break;
case '[':
if(s.top() == '{')
{
s.push(str[i]);
}
else{
flag = false;
goto FLAG;
}
break;
case']':
if(s.top() == '[')
{
s.pop();
}
else{
flag = false;
goto FLAG;
}
break;
case '{':
if(s.top() == '(')
{
s.push(str[i]);
}
else{
flag = false;
goto FLAG;
}
break;
case'}':
if(s.top() == '{')
{
s.pop();
}
else{
flag = false;
goto FLAG;
}
break;
}
}
FLAG:
if(flag && s.empty())cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}