当前位置:网站首页>5. 合法的括号串
5. 合法的括号串
2022-07-26 22:42:00 【lzl_0408】
【问题描述】
一个合法的括号串,是指只包含括号的串,如果满足如下条件:
(1)<> () [] {} 这四对括号是合法的;
(2)如果r是合法括号串,则<r> (r) [r] {r}也是;
(3)如果r,s是合法括号串,则rs也是;
所以<<>> , [<>{}(())],[({<>})]是合法的括号串,而)(,[( ])就不是。
【输入形式】
输入第一行正整数t (10 ≤ n ≤ 100),表示有多少组测试数据。
后面有t行,每行一个只包含8种括号符号的括号串。
40%的括号串的长度L 2 ≤ L≤ 20;
30%的括号串的长度L 2 ≤ L≤ 200;
20%的括号串的长度L 2 ≤ L≤ 2000;
10%的括号串的长度L 2 ≤ L≤ 20000;
【输出形式】
对于每组测试数据,如果括号串是合法的,输出“Yes”(输出没有引号)占一行,否则,输出“No”(输出没有引号)占一行。
【样例输入】
6
<<>>
)(
[<>{}(())]
[({<>})]
[(])
<([{
【样例输出】
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;
}边栏推荐
- New experience of mlvb cloud live broadcast: millisecond low latency live broadcast solution (with live broadcast performance comparison)
- SQL学习(1)——表相关操作
- Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=
- The difference between forward and redirect
- Flink based real-time computing Demo - Data Analysis on user behavior
- Spark----- RDD 的 Shuffle 和分区
- MySQL索引优化:哪些情况下需要建立索引(适合构建索引的几种情况)
- 堆排序相关知识总结
- Reasons why row locks in MySQL upgrade table locks
- The difference between golang slice make and new
猜你喜欢

基于Flink实时计算Demo:用户行为分析(四:在一段时间内到底有多少不同的用户访问了网站(UV))

SQL学习(3)——表的复杂查询与函数操作

Neo4j Basic Guide (installation, node and relationship data import, data query)

Game project export AAB package upload Google tips more than 150m solution

数据库表连接的简单解释

VSCode2015下编译darknet生成darknet.ext时error MSB3721:XXX已退出,返回代码为 1。

FaceNet

MySQL index optimization: under what circumstances do you need to build an index (several situations suitable for building an index)

Flink 滑动窗口理解&具体业务场景介绍

adb.exe已停止工作 弹窗问题
随机推荐
解决rsyslog服务占用内存过高
Analysis of contentvalues
Solve the problem of direct blue screen restart when VMware Workstation virtual machine starts
Understanding of Flink interval join source code
Cannot find a valid baseurl for repo: HDP-3.1-repo-1
Status management in Flink
Naive Bayes Multiclass训练模型
被围绕的区域
短视频App开发有哪些必备的功能?
Reasons why row locks in MySQL upgrade table locks
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)
腾讯云MLVB技术如何在移动直播服务中突出重围之基础概念
Deep understanding of golang - closures
MySQL uses and implements ranking functions rank and deny_ Rank and row_ NUMBER
Kubernetes 是什么 ?
Flink 滑动窗口理解&具体业务场景介绍
Flink sliding window understanding & introduction to specific business scenarios
One of the Flink requirements - sideoutput (Application of side output flow: output the temperature higher than 30 ℃ to the mainstream, and output the temperature lower than 30 ℃ to the side flow)
并发编程之生产者消费者模式
使用tika 判断文件类型