当前位置:网站首页>5. Legal bracket string
5. Legal bracket string
2022-07-27 01:18:00 【lzl_ 0408】
【 Problem description 】
A legal string of parentheses , A string that contains only parentheses , If the following conditions are met :
(1)<> () [] {} These four pairs of brackets are legal ;
(2) If r It's a string of legal brackets , be <r> (r) [r] {r} It's also ;
(3) If r,s It's a string of legal brackets , be rs It's also ;
therefore <<>> , [<>{}(())],[({<>})] Is a legal string of parentheses , and )(,[( ]) It's not .
【 Input form 】
Enter a positive integer in the first line t (10 ≤ n ≤ 100), Indicates how many sets of test data .
In the back t That's ok , Each line contains only 8 A string of parentheses .
40% The length of the string of parentheses L 2 ≤ L≤ 20;
30% The length of the string of parentheses L 2 ≤ L≤ 200;
20% The length of the string of parentheses L 2 ≤ L≤ 2000;
10% The length of the string of parentheses L 2 ≤ L≤ 20000;
【 Output form 】
For each group of test data , If the string of parentheses is legal , Output “Yes”( The output has no quotation marks ) Occupy a line , otherwise , Output “No”( The output has no quotation marks ) Occupy a line .
【 The sample input 】
6
<<>>
)(
[<>{}(())]
[({<>})]
[(])
<([{
【 Sample output 】
Yes No Yes Yes No No
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
int n;
cin>>n;
for(int k=0;k<n;k++)
{
string a;
cin>>a;
stack<char> s;
int l=a.length();
bool flag=true;
for(int i=0;i<l;i++)
{
if(a[i]=='}'||a[i]=='>'||a[i]==']'||a[i]==')')
{
if(s.empty())
{
flag=0;
break;
}
}
if(a[i]=='('||a[i]=='{'||a[i]=='['||a[i]=='<')
s.push(a[i]);
else if(a[i]==')')
{
if(s.top()=='(')
s.pop();
else
{
flag=0;
break;
}
}
else if(a[i]=='}')
{
if(s.top()=='{')
s.pop();
else
{
flag=0;
break;
}
}
else if(a[i]==']')
{
if(s.top()=='[')
s.pop();
else
{
flag=0;
break;
}
}
else if(a[i]=='>')
{
if(s.top()=='<')
s.pop();
else
{
flag=0;
break;
}
}
}
if(s.empty()&&flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}边栏推荐
- 7. F1方程式冠军
- Flink1.11 SQL local run demo & local webui visual solution
- 堆排序相关知识总结
- 被围绕的区域
- Tencent upgrades the live broadcast function of video Number applet. Tencent's foundation for continuous promotion of live broadcast is this technology called visual cube (mlvb)
- More than live streaming: what other eye-catching functions does Tencent cloud live mlvb plug-in have besides streaming / streaming
- 无重复字符的最长子串
- Doris or starrocks JMeter pressure measurement
- Understanding of Flink checkpoint source code
- 3. 拳王阿里
猜你喜欢

Cannot find a valid baseurl for repo: HDP-3.1-repo-1

集中式版本控制工具代码合并问题

腾讯云MLVB技术如何在移动直播服务中突出重围之基础概念

The difference between forward and redirect

SQL关系代数——除法

李宏毅机器学习(2017版)_P1-2:机器学习介绍

Li Hongyi machine learning (2017 Edition)_ P6-8: gradient descent

Play guest cloud brush machine 5.9

The dependency of POM file is invalid when idea imports external projects. Solution

MTCNN
随机推荐
堆排序相关知识总结
Android -- basic usage of litepal database framework
What are the necessary functions of short video app development?
Iptables detailed explanation and practical cases
小程序直播、连线直播、直播打赏:腾讯云移动直播组件MLVB多场景直播拓展
Li Hongyi machine learning (2017 Edition)_ P13: deep learning
Jenkins--基础--5.2--系统配置--系统配置
10 - deploy MySQL on CentOS 7
Come and help you understand the Internet of things in three minutes
李宏毅机器学习(2017版)_P1-2:机器学习介绍
微信大量下架数字藏品相关小程序:NFT产品究竟是未来还是陷阱?
Jenkins -- Basic -- 5.3 -- system configuration -- global security configuration
创建MDK工程
数据库期中(一)
李宏毅机器学习(2017版)_P21:卷积神经网络CNN
Come and help you understand the mobile Internet in a minute
adb. Exe stopped working popup problem
李宏毅机器学习(2017版)_P6-8:梯度下降
25 common questions in Flink interview (no answer)
Analysis of contentvalues