算法思想:
- 1、遍历输入的括号序列
- 2、如果当前遍历到左括号,则入栈
- 3、如果当前遍历到右括号,则出栈一个元素,看其是否与当前的右括号组成一对,如果不是,则匹配失败。或者在出栈过程中发生异常(从空栈中出栈),也匹配失败。易错点:"if右括号且栈不空,则pop; if右括号且栈空,则输出no"这种并行的写法是错误的,因为如果满足第一个条件,pop之后栈为空即又满足了第二个条件。正确的做法是将if右括号作为一个大的判断条件,再在其中判断栈是否为空,避免对元素的二次讨论。
另外,对栈是否为空的特判十分重要,如果栈空但进行pop操作,则会导致运行时错误(runtime error) - 4、若能顺利遍历完成,检查栈中是否还有剩余元素,如果有,则匹配失败;如果没有,则匹配成功。
例题一:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
stack<char> str;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '<' || s[i] == '(' || s[i] == '[' || s[i] == '{') {
str.push(s[i]);
}
else if (str.empty()) {
cout << "no";
return 0;
}//是右括号且此时为空栈,输出"no",直接return 0;
else if ((s[i] == ')' && str.top() == '(') || (s[i] == ']' && str.top() == '[') || (s[i] == '}' && str.top() == '{') || (s[i] == '>' && str.top() == '<')) {
str.pop();
} // 是右括号且与栈顶元素匹配,弹出。
else {
cout << "no";
return 0;
} //右括号与栈顶元素不匹配,输出"no".
}
if (str.empty()) cout << "yes";//栈空即为匹配,反之则不然
else cout << "no";
return 0;
}
例题二:
整体代码逻辑也与例题一大体相同。
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<char> str;
string s;
cin >> s;
for (int i = 0; i < s.size() && s[i] != '@'; i++) {
if (str.empty() && s[i] == ')') {
cout << "NO";
return 0;
}
else if (s[i] == '(') str.push(s[i]);
else if (s[i] == ')') str.pop();
}
if (str.empty()) cout << "YES";
else cout << "NO";
return 0;
}
【引申】
1、需要判断多次括号是否匹配
#include <bits/stdc++.h>
using namespace std;
int main(){
int n; //需要判断的字符串数
scanf("%d",&n);
while(n --){ //重复执行n次
//接下来差不多和上面的一样,那不一样的我会在旁边写注释哦~
string str;
stack <char> s;
bool flag = true; //这里是为了要判断是否已经输出过答案了,因为有多次要判断的地方,所以不能直接return 0,只能break,详细见后面
cin>>str;
for(int i = 0;i < str.length();i ++){
if(str[i] == '<' || str[i] == '(' || str[i] == '[' || str[i] == '{'){
s.push(str[i]);
}
else{
if(s.empty()){
cout<<"no"<<endl;
//这里就是来做以前的return 0 操作的代码
//因为这里有多个字符串需要匹配,所以不能直接return 0,并且break也只能退出一次循环,后面的判断还是会执行的,就会多输出一种方案,那这里就需要用到一个bool类型的变量来判断是否输出过了
flag = false;
break;
}
else if((str[i] == '>' && s.top() == '<') || (str[i] == ')' && s.top() == '(') || (str[i] == ']' && s.top() == '[') || (str[i] == '}' && s.top() == '{')) s.pop();
else{
//同上
cout<<"no"<<endl;
flag = false;
break;
}
}
}
if(s.empty() && flag) cout<<"yes"<<endl; //因为不能确定前面是否输出过,所以要判断一下flag是否为true(为输出过)
else if(flag) cout<<"no"<<endl;
}
return 0;
}
2、优先级判断的括号匹配,并且要判断好几次
题目:
字符串中只含有括号 ()(),[][],<><>,,判断输入的字符串中括号是否匹配。如果括号有互相包含的形式,从内到外必须是<><>,()(),[][],,例如。输入:[()][()] 输出:yesyes,而输入([])([]),()]都应该输出nono。
输入样例:
5
{}{}<><>()()[][]
{{}}{{}}<<>><<>>(())(())[[]][[]]
{{}}{{}}<<>><<>>(())(())[[]][[]]
{<>}{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]
><}{{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]
输出样例:
yes
yes
yes
yes
no
#include <iostream>
#include <map>
using namespace std;
const int N = 300;
int c[4];
struct Stack{
int length;
int a[N];
void create(){
length = 0;
}
int size(){
return length;
}
int empty(){
if(length == 0) return true;
else return false;
}
void push(int x){
a[++ length] = x;
}
int top(){
return a[length];
}
void pop(){
if(length) length --;
}
int seek(int n){
return a[n];
}
}s;
int main(){
int n;
scanf("%d",&n);
//要判断优先级的时候,有一个小技巧,就是把建一个数组,把每个括号的优先级存进去,我这里是数字越小,优先级越高
c['<'] = 1;
c['('] = 2;
c['['] = 3;
c['{'] = 4;
while(n --){
bool flag = true;
s.create();
string str;
cin>>str;
for(int i = 0;i < str.length();i ++){
if(str[i] == '<' || str[i] == '{' || str[i] == '(' || str[i] == '['){ //是左括号
if(c[str[i]] <= s.top() || s.empty()) s.push(c[str[i]]); //因为要判断优先级问题,如果这个左括号的优先级大于等于栈顶的括号的优先级(如果栈是空的,说明前面的已经匹配好了,或者是第一个字符,也是可以的),那就可以push进来
else{ //否则优先级错误,输出no
cout<<"no"<<endl;
flag = false;
break;
}
}
else{
if(s.empty()){
cout<<"no"<<endl;
flag = false;
break;
}
if((s.top() == c['('] && str[i] == ')') || (s.top() == c['['] && str[i] == ']') || (s.top() == c['<'] && str[i] == '>') || (s.top() == c['{'] && str[i] == '}')){
s.pop();
}
else{
cout<<"no"<<endl;
flag = false;
break;
}
}
}
if(s.empty() && flag) cout<<"yes"<<endl;
else if(flag) cout<<"no"<<endl;
}
return 0;
}