首页
DreamJudge
院校信息
考研初试
机试真题
讨论区
兑换中心
登录
注册
上岸
以下题解仅供学习参考使用。
抄袭、复制题解,以达到刷AC率/AC数量或其他目的的行为,在N诺是严格禁止的。
N诺非常重视学术诚信。此类行为将会导致您成为作弊者。具体细则请查看N诺社区规则。
leo110
2025年5月17日 16:11
括号匹配 题解:
P1501
回复 0
|
赞 0
|
浏览 198
//1067题的代码稍微改一点就行。主要两点不同:①只有两个类型的括号②括号没有优先级 //解题思路:map映射字符为整数,结合数据结构的stack进行括号匹配。 #include<iostream> #include<map> #include<stack> using namespace std; map<char,int> Bracket={{'[',-2},{'(',-1},{')',1},{']',2}}; bool match(co...
阿灿
2025年3月22日 22:45
括号匹配 题解:stack
P1501
回复 0
|
赞 6
|
浏览 396
#include<bits/stdc++.h> using namespace std; int main(){ string str; cin>>str; int n=str.length(); stack<char>st; for(int i=0;i<n;i++){ if(!st.empty()){ char now = st.top(); if(str[i]==')'&&now=='('||now=='['&&str[i]==']'){ st.pop...
wuhududu
2025年3月21日 19:18
括号匹配 题解:
P1501
回复 0
|
赞 2
|
浏览 406
//一定要注意先输入 ')' 或者 ']' 的情况,此时正好栈为空,这个时候检查栈顶元素是不是 '(' 就会出现错误,这是由于栈空时检查栈顶元素是非法的,故在初始化栈的时候就先push一个不会影响括号匹配的元素,保证先输入 ')'不会出现非法的情况; #include<bits/stdc++.h> using namespace std; char s[10000]; int main(){ gets(s); ...
shiv15832
2025年3月16日 15:45
括号匹配 题解:简单之中亦有坑
P1501
回复 0
|
赞 7
|
浏览 488
基本思路:如果是左括号,入栈。如果是右括号,与栈顶匹配,成功栈顶出栈,失败怎么办?如果把匹配过程单独写成一个函数Judge,那就可以直接return flase;如果匹配过程写在main函数里,那就有些麻烦,自己写一写就知道需要引入一些bool变量作为标记,而且可能面临着输出多个NO的情况,大家做题的时候,一条路走着走着越来越麻烦,不如回头在看一看有没有更好的路子。 这道题最重要的一点:如果是右括号,不能仅仅与栈顶匹配,必须要先判定栈是否为空!eg:输入】】【,不加判空就会导致本轮循环空过导致卡75%!不过这一点也是基于代码的不同因人而异,所以我说本题简单中亦有坑,每个人的思路不一...
西电机试专家
2025年2月16日 12:04
括号匹配 题解:用数组来模拟栈+知识点总结图示
P1501
回复 0
|
赞 4
|
浏览 482
#include<bits/stdc++.h> using namespace std; int main(){ string str;//初始字符串 cin>>str; char s[1000];//s为结果处理栈 char a; int top=-1,flag=0;//0说明YES &...
15240928957
2024年3月20日 21:26
Runtime Error 通过75%
P1501
回复 3
|
赞 8
|
浏览 1.1k
#include <iostream> #include <stack> #include <cstring> using namespace std; int main() { stack<int> st; char s[10005]; cin >> s; for (int i = 0; i < strlen(s); i++) &n...
Candour
2024年5月6日 23:30
括号匹配(C++ string栈) 题解:
P1501
回复 0
|
赞 11
|
浏览 954
#include <bits/stdc++.h> using namespace std; string str; int main() { cin >> str; string stk1, stk2; for(int i = 0; i < str.size(); i ++) { if(str[i] == '(') stk1.push_back(')'); else if(str[i] == '[') stk1.push_back(']'); else if(str[i] == ')') stk2....
easymoney
2024年3月22日 15:27
括号匹配 题解:
P1501
回复 0
|
赞 3
|
浏览 1.0k
#include <stdio.h> #include <iostream> #include <algorithm> #include <stack> #include <string.h> using namespace std; int main(){ char s[1000]; cin >> s; int len =strlen(s); stack<char> st; for(int i = 0;i <len;i++){ if(!st.empty()...
Śś
2024年3月18日 17:42
括号匹配 题解:画两个栈演示一下可能会比较形象
P1501
回复 0
|
赞 0
|
浏览 661
#include<iostream> #include<stack> #include<cstring> using namespace std; stack<char> s1,s2; bool Judge(char a,char b) { if(a=='('&&b==')') return true; if(a=...
FIVEszc
2024年3月15日 16:05
括号匹配 题解:求大佬解惑,显示runtime error,75%数据
P1501
回复 4
|
赞 4
|
浏览 1.2k
#include <bits/stdc++.h> using namespace std; int main() { stack <char> mystack; char s[1000]; int tag=0; scanf("%s",s); int len=strlen(s); for(int j=0;j<len;j++) { if(s[j]=='('||s[j]=='[') mystack.push(s[j]); ...
1
2
3
题目
括号匹配
题解数量
25
发布题解
在线答疑
热门题解
1
括号匹配(C++ string栈) 题解:
2
1501 括号匹配 栈经典题
3
Runtime Error 通过75%
4
括号匹配 题解:简单之中亦有坑
5
括号匹配 题解:stack
6
括号匹配 题解:用数组来模拟栈+知识点总结图示
7
括号匹配 题解:求大佬解惑,显示runtime error,75%数据通过
8
括号匹配 题解:
9
括号匹配(仅有[]与())
10
括号匹配 题解: