当前位置:网站首页>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;
}边栏推荐
- How to update PIP3? And running PIP as the 'root' user can result in broken permissions and conflicting behavior
- pytorch_demo1
- [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
- Development of three database general SQL code based on PG Oracle and MySQL
- 一段平平无奇的秋招经历
- [resolved] SSO forwarding succeeded, and there was an unexpected error (type=internal server error, status=500) caused by parameters in the forwarding URL
- Stored procedure test 1 -- first acquaintance of love
- Mqtt instruction send receive request subscription
- Leetcode54. Spiral matrix
- idea远程调试
猜你喜欢

PHP realizes data interaction with MySQL

Attack and defense world MFW

Plato farm is expected to further expand its ecosystem through elephant swap

2020国际机器翻译大赛:火山翻译力夺五项冠军

Abstract factory pattern

Idea remote debugging

2020 International Machine Translation Competition: Volcano translation won five championships

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

Use of "PHP Basics" Boolean

如何更新pip3?和Running pip as the ‘root‘ user can result in broken permissions and conflicting behaviour
随机推荐
The dragon lizard exhibition area plays a new trick this time. Let's see whose DNA moved?
Data extraction 1
pytorch_demo1
Lu Xun: I don't remember saying it, or you can check it yourself!
一文速览EMNLP 2020中的Transformer量化论文
The third letter to the little sister of the test | Oracle stored procedure knowledge sharing and test instructions
Digital transformation driven by enterprise architecture!
XxE & XML vulnerability
CommonTitleBar hide left right
3D laser slam: Interpretation of logo-loam paper --- Abstract
How to play with the purchase of SAP variant materials? Look at this article and you will understand
Development of three database general SQL code based on PG Oracle and MySQL
opengauss从库停掉,发现主库无法写入数据
Vcenter7.0 managing esxi7.0 hosts
2020国际机器翻译大赛:火山翻译力夺五项冠军
On data security
Lua stateful iterator
Dasctf2022.07 enabling game password WP
DEMO:ST05 找文本ID 信息
How does kettle handle text data transfer as' 'instead of null