当前位置:网站首页>信息学奥赛一本通 1354:括弧匹配检验
信息学奥赛一本通 1354:括弧匹配检验
2022-06-26 07:36:00 【君义_noip】
【题目链接】
【题目考点】
1. 栈
【解题思路】
遍历字符串,左括号入栈,遇到右括号时,看栈顶括号与右括号是否能配对。如果两个括号都是小括号或中括号,那么二者配对,左括号出栈。否则括号不匹配。
如果遍历到右括号时,发现栈空,那么存在右括号无法匹配。
如果遍历结束时,栈不为空,那么存在左括号无法匹配。
【题解代码】
解法1:用数组及表达式实现栈
#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] == '[')//左括号入栈
stk[++top] = s[i];
else if(s[i] == ')' || s[i] == ']')
{
if(top == 0 ||
s[i] == ')' && stk[top] != '(' ||
s[i] == ']' && stk[top] != '[')//栈空或括号不配对
{
printf("Wrong");//无法配对
return 0;
}
else
top--;
}
}
if(top == 0)//如果栈不空,则无法配对
printf("OK");
else
printf("Wrong");
return 0;
}
解法2:使用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] == '[')//左括号入栈
stk.push(s[i]);
else if(s[i] == ')' || s[i] == ']')
{
if(stk.empty() ||
s[i] == ')' && stk.top() != '(' ||
s[i] == ']' && stk.top() != '[')//栈空或括号不配对
{
cout << "Wrong";//无法配对
return 0;
}
else
stk.pop();
}
}
if(stk.empty())//如果栈不空,则无法配对
cout << "OK";
else
cout << "Wrong";
return 0;
}
边栏推荐
- 快速找到优质对象的5种渠道,赶紧收藏少走弯路
- systemctl php配置文件
- PXRD, IR, TGA of two-dimensional porphyrin COF (POR COF) /cof (2D pdpor COF) - supplied by Qiyue
- GMP model
- The performance of iron and steel enterprises was expected to be good in January this year. Since February, the prices of products of iron and steel enterprises have increased significantly. A mighty
- Teach you how to use the harmonyos local simulator
- [UVM basics] connect of UVM_ Phase execution sequence
- Take you three minutes to get started typescript
- Redis (4) -- Talking about integer set
- A bold sounding and awesome operation - remake a Netflix
猜你喜欢

Sanic based services use celery to complete dynamic modification timing tasks

Oracle中计算除法——解决除数为零报错

How to quickly merge multiple PDF files?

Redis系列——redis启动,客户端day1-2
![[recommend an entity class conversion tool mapstruct, which is powerful and easy to use]](/img/7b/43becce42192fb5e0469465aa27a36.png)
[recommend an entity class conversion tool mapstruct, which is powerful and easy to use]

Median segmentation (find rules) - Niuke

Junit

Crosslinked porphyrin based polyimide ppbpi-2, ppbpi-1-cr and ppbpi-2-cr; Porous porphyrin based hyperbranched polyimide (ppbpi-1, ppbpi-2) supplied by Qiyue

Cache usage

5,10,15,20-tetraphenylporphyrin (TPP) and metal complexes fetpp/mntpp/cutpp/zntpp/nitpp/cotpp/pttpp/pdtpp/cdtpp supplied by Qiyue
随机推荐
Apache inlong graduated as a top-level project with a million billion level data stream processing capability!
SQL
Exploration and practice of incremental data Lake in station B
Two models of OSPF planning: double tower Raider and dog tooth crisscross
[UVM practice] Chapter 3: UVM Fundamentals (3) field automation mechanism
Ppbpi-h-cr, ppbpimn Cr, ppbpi Fe Cr alkynyl crosslinked porphyrin based polyimide material Qiyue porphyrin reagent
Oracle中计算除法——解决除数为零报错
C implementation adds a progress bar display effect to the specified column of the GridView table in devaxpress - code implementation method
Summary of domestic database examination data (continuously updated)
Request&Response
Junit
Jemter stress test - Basic request - [teaching]
Redis (5) -- Talking about compressed list
[North Asia data recovery] a server data recovery method in which the partitions in the RAID5 array are formatted due to the misoperation of the NTFS file system
CMDA 3634 image processing
QTreeWidget And QTableWidget
执行npm install -g serve时报错权限权限问题解决方案
Dark red crystal meso-5,10,15,20-tetra (p-aminophenyl) cobalt porphyrin (co TAPP); Meso-5,10,15,20-tetra (p-aminophenyl) cobalt porphyrin no complex (TAPP co no) supplied by Qiyue
My colleague asked a question I never thought about. Why did kubernetes' superfluous' launch the static pod concept?
The difference between insert ignore and insert into