当前位置:网站首页>Informatics Orsay all in one 1353: expression bracket matching | Luogu p1739 expression bracket matching
Informatics Orsay all in one 1353: expression bracket matching | Luogu p1739 expression bracket matching
2022-06-25 23:30:00 【Junyi_ noip】
【 Topic link 】
ybt 1353: Expression brackets match (stack)
Luogu P1739 Expression brackets match
【 Topic test site 】
1. Stack
【 Their thinking 】
Traversing the entire string , When an open bracket is encountered, it is pushed to the stack , When the right bracket is encountered , A left parenthesis should appear on the stack . If the stack is empty when the closing bracket is encountered , Description there are closing parentheses that cannot be paired .
At the end of the traversal , If the stack is not empty , Description there are left parentheses that cannot be paired .
You can use arrays and expressions to implement stack functions , You can use functions to implement stack functions , You can write your own stack class , You can also use STL Medium is stack.
【 Solution code 】
solution 1: Stack with array and expression
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[260];
char stk[260];
int top = 0;
scanf("%s", s);
bool isMatch = true;
for(int i = 0; s[i] != '@'; ++i)
{
if(s[i] == '(')
stk[++top] = s[i];// Push
else if(s[i] == ')')
{
if(top == 0)// Judge whether the stack is empty . Empty stack encountered with closing parenthesis
{
isMatch = false;
break;
}
else
--top;// Out of the stack
}
}
if(top != 0)// If the last stack is not empty
isMatch = false;
if(isMatch)
cout << "YES";
else
cout << "NO";
return 0;
}
solution 2: Function to realize stack function
#include<bits/stdc++.h>
using namespace std;
char stk[260];
int top = 0;
void push(char c)
{
stk[++top] = c;
}
bool empty()
{
return top == 0;
}
void pop()
{
top--;
}
int main()
{
char s[260];
scanf("%s", s);
bool isMatch = true;
for(int i = 0; s[i] != '@'; ++i)
{
if(s[i] == '(')
push(s[i]);// Push
else if(s[i] == ')')
{
if(empty())// Judge whether the stack is empty . Empty stack encountered with closing parenthesis
{
isMatch = false;
break;
}
else
pop();// Out of the stack
}
}
if(empty() == false)// If the last stack is not empty
isMatch = false;
if(isMatch)
cout << "YES";
else
cout << "NO";
return 0;
}
solution 3:C++STL Realize stack function
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[260];
scanf("%s", s);
stack<char> stk;
bool isMatch = true;
for(int i = 0; s[i] != '@'; ++i)
{
if(s[i] == '(')
stk.push(s[i]);// Push
else if(s[i] == ')')
{
if(stk.empty())// Judge whether the stack is empty . Empty stack encountered with closing parenthesis
{
isMatch = false;
break;
}
else
stk.pop();// Out of the stack
}
}
if(stk.empty() == false)// If the last stack is not empty
isMatch = false;
if(isMatch)
cout << "YES";
else
cout << "NO";
return 0;
}
边栏推荐
- What is Unified Extensible Firmware Interface (UEFI)?
- Actual combat: how to quickly change font color in typera (blog sharing - perfect) -2022.6.25 (solved)
- Determine whether the appointment time has expired
- 【无标题】打开一个项目连接,无法正常显示时,ping一下ip
- Es7/es9 -- new features and regularities
- cookie、session、token
- Day4 branch and loop summary and operation
- Windows redis installation and simple use
- 2. What is the geometric meaning of a vector multiplying its transpose?
- 记一次beego通过go get命令后找不到bee.exe的坑
猜你喜欢

Pointer strengthening and improvement

ES6 - numerical extension and object extension

转载: QTableWidget详解(样式、右键菜单、表头塌陷、多选等)

电路模块分析练习5(电源)

UE4 学习记录一 创建角色,并控制其移动

Rk3568+ Hongmeng industrial control board industrial gateway video gateway solution

Use and difference between ue4\ue5 blueprint node delay and retroggable delay

【无标题】打开一个项目连接,无法正常显示时,ping一下ip

Circuit module analysis exercise 6 (switch)

做接口测试,这3种工具到底什么时候用?
随机推荐
CDN加速是什么
【opencv450-samples】读取图像路径列表并保持比例显示
元宇宙标准论坛成立
Flex & Bison Start
[Axi] interpretation of Axi protocol disorder mechanism
jdbc常见异常及错误解决办法汇总
【2023校招刷题】番外篇1:度量科技FPGA岗(大致解析版)
[opencv450 samples] read the image path list and maintain the proportional display
关于go中资源泄漏/goroutine泄漏/内存泄漏/CPU打满等情况分析
Es7/es9 -- new features and regularities
指针强化与提高
Determine whether the appointment time has expired
Qt 中文和英文分别使用不同的字体
漏刻有时API接口实战开发系列(13):小鹅通云服务PHP-API二维数组传参解决方案
ES6 - numerical extension and object extension
Transformers load pre training model
Circuit module analysis exercise 5 (power supply)
Live800在线客服系统:跨越时空做生意,从每次互动开始
软件测试面试一直挂,面试官总是说逻辑思维混乱,怎么办?
分享一个OSGeo4W64下载好的库,基于qgis3.10的