当前位置:网站首页>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;
}边栏推荐
- MYSQL中的行锁升级表锁的原因
- 非递归前中后序遍历二叉树
- Neo4j基础指南(安装,节点和关系数据导入,数据查询)
- pytorch张量数据基础操作
- 基于Flink实时计算Demo—关于用户行为的数据分析
- VSCode2015下编译darknet生成darknet.ext时error MSB3721:XXX已退出,返回代码为 1。
- 李宏毅机器学习(2017版)_P1-2:机器学习介绍
- What are the necessary functions of short video app development?
- Android -- Data Persistence Technology (III) database storage
- 无重复字符的最长子串
猜你喜欢

Flink based real-time computing Demo - Data Analysis on user behavior

短视频App开发有哪些必备的功能?

Real time calculation demo based on Flink: user behavior analysis (IV: how many different users have visited the website (UV) in a period of time)

redis——缓存雪崩、缓存穿透、缓存击穿

Scala-模式匹配

李宏毅机器学习(2017版)_P14:反向传播

Redis -- cache avalanche, cache penetration, cache breakdown

Spark----- RDD 的 Shuffle 和分区

Redisson working principle - source code analysis

腾讯云MLVB技术如何在移动直播服务中突出重围之基础概念
随机推荐
视频类小程序变现的最短路径:从带货到品牌营销
无重复字符的最长子串
flink1.11 sql本地运行demo & 本地webUI可视解决
Isolation level of MySQL database transactions (detailed explanation)
Real time calculation demo based on Flink: user behavior analysis (IV: how many different users have visited the website (UV) in a period of time)
Select query topic exercise
flink需求之—ProcessFunction(需求:如果30秒内温度连续上升就报警)
MTCNN
Applet live broadcast, online live broadcast, live broadcast reward: Tencent cloud mobile live broadcast component mlvb multi scene live broadcast expansion
The shortest way to realize video applets: from bringing goods to brand marketing
Flink 1.15 local cluster deployment standalone mode (independent cluster mode)
More than live streaming: what other eye-catching functions does Tencent cloud live mlvb plug-in have besides streaming / streaming
New experience of mlvb cloud live broadcast: millisecond low latency live broadcast solution (with live broadcast performance comparison)
深度学习汇报(1)
Flink sliding window understanding & introduction to specific business scenarios
FlinkSql多表(三表) join/interval join
Game project export AAB package upload Google tips more than 150m solution
ADB shell screen capture command
Programming method of sparksql
What is kubernetes?