当前位置:网站首页>信息学奥赛一本通 1355:字符串匹配问题(strs)
信息学奥赛一本通 1355:字符串匹配问题(strs)
2022-06-26 07:36:00 【君义_noip】
【题目链接】
【题目考点】
1. 栈
【解题思路】
设数组pri,pri[括号字符]是个整数表示括号的优先级。优先级从高到低为:<([{ ,
这里设pri[右括号] = 10*pri[左括号],同类的左右括号的优先级数值是10倍关系,这样就可以用pri来判断两个括号是否配对。
遍历整个字符串
- 如果遇到左括号:只有栈空或当前左括号优先级大于等于栈顶左括号优先级时,左括号入栈。否则左括号优先级混乱,不匹配。
- 如果遇到右括号:如果栈空,则不匹配。如果栈不空,看栈顶左括号能否与该右括号配对,如果可以则出栈,否则不匹配。
结束遍历字符串后,看栈是否为空,如果不为空,则不匹配。
【题解代码】
解法1:
#include<bits/stdc++.h>
using namespace std;
int pri[128];//括号优先级 小括号优先级高,大括号优先级低。pri[右括号] = 10*pri[左括号]
void init()
{
pri['{'] = 1;
pri['['] = 2;
pri['('] = 3;
pri['<'] = 4;
pri['}'] = 10;
pri[']'] = 20;
pri[')'] = 30;
pri['>'] = 40;
}
int main()
{
init();
bool isMatch;
int n;
string s;
stack<char> stk;
cin >> n;
while(n--){
isMatch = true;
stk = stack<char>();//清空栈
cin >> s;
for(int i = 0; i < s.length(); ++i){
if(stk.empty() || pri[s[i]] <= 4 && pri[s[i]] >= pri[stk.top()])//如果栈空或要入栈的左括号优先级更高
stk.push(s[i]);//入栈
else if(pri[s[i]] == 10*pri[stk.top()])//左括号优先级乘10等于右括号,即左右括号配对
stk.pop();//出栈
else//左右括号不匹配 或 栈空
{
isMatch = false;
break;
}
}
if(stk.empty() == false)
isMatch = false;
if(isMatch)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
边栏推荐
- GMP model
- $a && $b = $c what???
- Niuniu looks at the cloud (greedy, hash, push formula) - the first session of Niuke winter vacation training camp
- Go language custom DNS resolver practice yyds dry inventory
- 3,3 '- di (3,4-dicarboxyphenoxy) -4,4' - diphenylethynylbiphenyldianhydride (bpebpda) / porphyrin 2dcofs (H2P COF, ZNP COF and cup COF) supplied by Qiyue
- Encapsulating ranging and surface class based on leaflet
- Redis series - five common data types day1-3
- How to convert Unicode into Chinese characters in Excel
- 少年,你可知 Kotlin 协程最初的样子?
- C implementation adds a progress bar display effect to the specified column of the GridView table in devaxpress - code implementation method
猜你喜欢

Kalman filter_ Recursive Processing

Important reference indicators for data center disaster recovery: RTO and RPO

oracle创建带返回值的存储过程并sql执行调用
![[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](/img/4d/01310b489ca6a599a125e849ae4856.jpg)
[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

How MySQL implements the RC transaction isolation level

Niuniu looks at the cloud (greedy, hash, push formula) - the first session of Niuke winter vacation training camp

Oracle creates stored procedures with return values and executes SQL calls

C#实现给DevExpress中GridView表格指定列添加进度条显示效果——代码实现方式

Oracle中计算除法——解决除数为零报错
![Jemter stress test - visualization tool support - [installation]](/img/e9/9acda4e37c98cc21df9499684205c6.png)
Jemter stress test - visualization tool support - [installation]
随机推荐
Calculate division in Oracle - solve the error report when the divisor is zero
Database persistence
Jemter 压力测试 -可视化工具支持-【安装篇】
Quickly find five channels for high-quality objects, quickly collect and avoid detours
Liangshui Xianmu shows his personal awareness as a unity3d worker
Yyds dry inventory Druid connection pool usage
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
The difference between insert ignore and insert into
In interface testing, several methods to verify the success of deleting interfaces
Two models of OSPF planning: double tower Raider and dog tooth crisscross
If you don't understand, please hit me
QTreeWidget And QTableWidget
MySQL storage and custom functions
Jemter stress test - Basic request - [teaching]
Error: the specified LINQ expression contains a reference to a query associated with a different context
Detailed materials of applying for residence permit in non local Beijing
5,10,15,20-tetra (4-bromophenyl) porphyrin (h2tppbr4) /5.2.15,10,15,20-tetra [4-[(3-aminophenyl) ethynyl] phenyl] porphyrin (tapepp) Qiyue porphyrin reagent
Error reported by using two-dimensional array [[]] in thymeleaf: could not parse as expression
Children play games (greed, prefix and) - Niuke winter vacation training camp
MXNet对NIN网络中的网络的实现