当前位置:网站首页>All in one 1353 -- expression bracket matching (stack)
All in one 1353 -- expression bracket matching (stack)
2022-07-27 08:14:00 【Bamboo monk-】
Topic website
http://ybt.ssoier.cn:8088/problem_show.php?pid=1353
【 Title Description 】
Let's say an expression has letters ( A lowercase letter )、 Operator (+,-,∗,/) And left and right little ( round ) Bracket construction , With “@” As the terminator of an expression . Please write a program to check whether the left and right parentheses in the expression match , If the match , Then return to “YES”; Otherwise return to “NO”. The length of the expression is less than 255, The left parenthesis is less than 20 individual .
【 Input 】
A line of data , That's the expression .
【 Output 】
a line , namely “YES” or “NO”.
【 sample input 】
2*(x+y)/(1-x)@
【 sample output 】
YES
【 Tips 】
【 The sample input 2】
(25+x)*(a*(a+b+b)@
【 Sample output 2】
NO
Examine the subject
Expression has no spaces , direct cin that will do . Numbers , Symbol , Letters have no effect on the title
Ideas
1. Judge each character in turn
2. If the current character is @, Exit loop
3. If the current character is an open parenthesis , Push
4. If it's right bracket , Determine whether there is an open bracket in the stack . without , Note that there is no left parenthesis to match it , Output NO; Otherwise, let an open bracket come out of the stack , Indicates that it has been paired
for(int i=0;i<str.size();i++)
{
if(str[i]=='@') break;
if(str[i]=='(')stk.push(str[i]);
if(str[i]==')'){
if(stk.empty())
{
cout<<"NO";
return 0;
}
stk.pop();
}
}5. After the loop ends, judge whether the stack is empty . If it's not empty , Note that there are still left parentheses that have not been matched , Output NO, Otherwise output YES
if(stk.empty()) {
cout<<"YES";
}
else{
cout<<"NO";
return 0;
}Complete code
#include<bits/stdc++.h>
using namespace std;
string str;
stack<char>stk;
int main()
{
cin>>str;
for(int i=0;i<str.size();i++)
{
if(str[i]=='@') break;
if(str[i]=='(')stk.push(str[i]);
if(str[i]==')'){
if(stk.empty())
{
cout<<"NO";
return 0;
}
stk.pop();
}
}
if(stk.empty()) {
cout<<"YES";
}
else
cout<<"NO";
return 0;
}边栏推荐
- 擎创科技加入龙蜥社区,共建智能运维平台新生态
- Demo:st05 find text ID information
- containerd拉取私库镜像失败(kubelet)
- End of year summary
- Shell scripts related
- Kalibr calibration realsensed435i -- multi camera calibration
- Use of NPM
- vCenter7.0管理Esxi7.0主机
- Graph node deployment and testing
- The third letter to the little sister of the test | Oracle stored procedure knowledge sharing and test instructions
猜你喜欢

一文速览EMNLP 2020中的Transformer量化论文

Enhancement: BTE process introduction

Use of "PHP Basics" Boolean

Promise details

PHP realizes data interaction with MySQL

The seta 2020 international academic conference will be held soon. Welcome to attend!

Use of string type "PHP Basics"

Promise详解

QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform

The dragon lizard exhibition area plays a new trick this time. Let's see whose DNA moved?
随机推荐
How to analyze and locate problems in 60 seconds?
"Intermediate and advanced test questions": what is the implementation principle of mvcc?
Leetcode56. Consolidation interval
"PHP Basics" uses echo statements to output information
I can't figure out why MySQL uses b+ trees for indexing?
Stored procedures and functions
SSTI template injection
PHP realizes data interaction with MySQL
Download and usage of sequel Pro
On data security
DEMO:ST05 找文本ID 信息
Netdata 性能监测工具介绍、安装、使用
[resolved] the new version of pychart (2022) connects to the server to upload files and reports an error of "command Rsync is not found in path", and the files cannot be synchronized
代码接口自动化的有点
Stored procedure test 1 -- first acquaintance of love
Translation character '/b' in C #
opengauss从库停掉,发现主库无法写入数据
The code interface is a little automated
How to play with the purchase of SAP variant materials? Look at this article and you will understand
XxE & XML vulnerability