当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- MXNet对NIN网络中的网络的实现
- JMeter stress test web agent local interface test [teaching]
- Flutter (III) - master the usage of dart language in an article
- Exploration and practice of incremental data Lake in station B
- SQL
- MySQL 'replace into' 的坑 自增id,备机会有问题
- [UVM practice] Chapter 2: a simple UVM verification platform (4) the ultimate masterpiece of UVM: sequence
- Children play games (greed, prefix and) - Niuke winter vacation training camp
- Jemter 压力测试 -基础请求-【教学篇】
- Kalman filter_ Recursive Processing
猜你喜欢

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

Solution to the permission problem when NPM install -g serve reports an error

数据中心灾难恢复的重要参考指标:RTO和RPO
![[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]
![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](/img/2b/837dfa0f80c31c2903927085c491df.jpg)
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

The first multi platform webcast of 2021ccf award ceremony pays tribute to the winners! CCF outstanding engineer

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

Cloud native integration data warehouse heavy release

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
![Jemter stress test - visualization tool - [usage]](/img/b1/3c367b690bbc16ae5a3e9e2c85af73.png)
Jemter stress test - visualization tool - [usage]
随机推荐
Children play games (greed, prefix and) - Niuke winter vacation training camp
Is it legal to open an account for compass stock trading software? Is it safe?
ES cluster_ block_ exception read_ only_ allow_ Delete question
Jemter stress test - visualization tool support - [installation]
Jemter stress test - visualization tool - [usage]
Jemter stress test - Basic request - [teaching]
Golang源码包集合
Go language custom DNS resolver practice yyds dry inventory
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
Median segmentation (find rules) - Niuke
C#/. Net phase VI 01C Foundation_ 02:vs2019 basic operations, excluding code files, smart tips, data types, differences between float and double, and differences between string and string
Jemter stress test - basic requirements - [teaching]
少年,你可知 Kotlin 协程最初的样子?
手机开户哪个证券公司佣金最低?网上开户是否安全么?
Alkynyl crosslinked porphyrin based polyimide materials (ppbpi-h-cr, ppbpi Mn cr.ppbpi Fe Cr); Metalloporphyrin based polyimide (ppbpi Mn, ppbpi FE) supplied by Qiyue
Tsinghua Yaoban chendanqi won Sloan award! He is a classmate with last year's winner Ma Tengyu. His doctoral thesis is one of the hottest in the past decade
Two models of OSPF planning: double tower Raider and dog tooth crisscross
Es performance tuning and other features
Junit
Multisensor fusion sensing