当前位置:网站首页>Informatics Orsay all in one 1354: bracket matching test
Informatics Orsay all in one 1354: bracket matching test
2022-06-26 07:39:00 【Junyi_ noip】
【 Topic link 】
【 Topic test site 】
1. Stack
【 Their thinking 】
Traversal string , Left bracket in stack , When the right bracket is encountered , See if the top bracket and the right bracket can be paired . If both parentheses are parentheses or brackets , Then the two are paired , Left bracket out of stack . Otherwise, the brackets don't match .
If you traverse to the closing parenthesis , Stack empty found , Then there are closing parentheses that cannot be matched .
If at the end of the traversal , Stack is not empty. , Then there are left parentheses that cannot be matched .
【 Solution code 】
solution 1: Stack with array and expression
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[260], stk[260];
scanf("%s", s);
int top = 0, len;
len = strlen(s);
for(int i = 0; i < len; ++i)
{
if(s[i] == '(' || s[i] == '[')// Left bracket in stack
stk[++top] = s[i];
else if(s[i] == ')' || s[i] == ']')
{
if(top == 0 ||
s[i] == ')' && stk[top] != '(' ||
s[i] == ']' && stk[top] != '[')// Empty stack or unmatched parentheses
{
printf("Wrong");// Unable to pair
return 0;
}
else
top--;
}
}
if(top == 0)// If the stack is not empty , Cannot pair
printf("OK");
else
printf("Wrong");
return 0;
}
solution 2: Use C++ STL
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin >> s;
stack<char> stk;
for(int i = 0; i < s.length(); ++i)
{
if(s[i] == '(' || s[i] == '[')// Left bracket in stack
stk.push(s[i]);
else if(s[i] == ')' || s[i] == ']')
{
if(stk.empty() ||
s[i] == ')' && stk.top() != '(' ||
s[i] == ']' && stk.top() != '[')// Empty stack or unmatched parentheses
{
cout << "Wrong";// Unable to pair
return 0;
}
else
stk.pop();
}
}
if(stk.empty())// If the stack is not empty , Cannot pair
cout << "OK";
else
cout << "Wrong";
return 0;
}
边栏推荐
- SQL
- [UVM practice] Chapter 2: a simple UVM verification platform (5) build test cases
- 多传感器融合感知
- Jemter 壓力測試 -基礎請求-【教學篇】
- Systemctl PHP configuration file
- Calculate division in Oracle - solve the error report when the divisor is zero
- Flutter (III) - master the usage of dart language in an article
- Installation homebrew error summary
- Multisensor fusion sensing
- Yyds dry inventory executor package (executor function)
猜你喜欢
Web technology sharing | webrtc recording video stream
What is the difference between bone conduction earphones and ordinary earphones? Advantages of bone conduction earphones
Kalman filter_ Recursive Processing
Request&Response
OSPF design principles, commands take H3C as an example
Basic use of swiperefreshlayout, local refresh of flutterprovider
In interface testing, several methods to verify the success of deleting interfaces
Tetra - (4-pyridyl) porphyrin tpyp and metal complexes zntpyp/fetpyp/mntpyp/cutpyp/nitpyp/cotpyp/ptpyp/pdtpyp/cdtpyp (supplied by Qiyue porphyrin)
Tetradecanoxy tetraphenylporphyrin methacrylate mm-tpp-14c; Cetanoxy tetraphenyl porphyrin methacrylate mm-tpp-16c; Purple solid; Qiyue supply
Jemter stress test - Basic request - [teaching]
随机推荐
GMP model
Median segmentation (find rules) - Niuke
How can I find the completely deleted photos in Apple mobile phone?
手机开户哪个证券公司佣金最低?网上开户是否安全么?
My colleague asked a question I never thought about. Why did kubernetes' superfluous' launch the static pod concept?
Introduction to mapping in ES
SQL
Jemter 壓力測試 -基礎請求-【教學篇】
个人用指南针软件买股票安全吗?怎么炒股买股票呢
Niuniu looks at the cloud (greedy, hash, push formula) - the first session of Niuke winter vacation training camp
[NLP] vector retrieval model landing: Bottleneck and solution!
数据中心灾难恢复的重要参考指标:RTO和RPO
[recommend an entity class conversion tool mapstruct, which is powerful and easy to use]
Jemter stress test - visualization tool - [usage]
QT basics tutorial: qstring
Jemter stress test - Basic request - [teaching]
执行npm install -g serve时报错权限权限问题解决方案
Quickly find five channels for high-quality objects, quickly collect and avoid detours
指南针炒股软件开户是合法的吗?安全吗
Is it legal to open an account for compass stock trading software? Is it safe?