ZISUOJ 数据结构-栈

题目列表:

问题 A: 数据结构-栈-括号匹配

思路:

        遇到左半边括号,将其入栈,遇到右半边括号,则先判断栈是否为空,若为空,则匹配失败,若不为空,则再判断栈顶元素是否是与之匹配的左半边括号,若不是,则匹配失败,一直匹配到栈空,如果栈空,则匹配成功,否则匹配失败

参考题解:

#include<bits/stdc++.h>
using namespace std;
stack<char> stk;
string line;
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	getline(cin,line);
	for(auto c:line){
		if(c=='(') stk.push(c);
		else if(c==')'){
			if(stk.empty()){
				cout << "NO\n";
				return 0; 
			}else{
				if(stk.top()=='(') stk.pop();
			}
		}
	}
	cout << (stk.empty()?"YES\n":"NO\n");
	return 0;
}

问题 B: 数据结构-栈-车厢顺序

思路:

        这个题也不知道怎么说思路,反正是很经典的一个题,如果实在不懂,可以去查一查资料。

参考题解:
 

#include<bits/stdc++.h>
using namespace std;
constexpr int MAXN = 1e3+5;
int n,cnt = 1;
array<int,MAXN> a;
stack<int> stk;
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin >> n;
	for(int i = 0;i<n;i++) cin >> a[i];
	for(int i = 0;i<n;i++){
		while(cnt<=a[i]){
			stk.push(cnt++);
		}
		if(stk.top()==a[i]) stk.pop();
		else{
			cout << "NO\n";
			return 0;
		}
	}
	cout << "YES\n";
	return 0;
}

问题 C: 数据结构-栈-括弧匹配检验

思路:

        跟第一题相似,注意括号对应匹配即可。

参考题解:

#include<bits/stdc++.h>
using namespace std;
stack<char> stk;
string line;
unordered_map<char,char> mp = {{'(',')'},{'[',']'}};
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	getline(cin,line);
	for(auto c:line){
		if(c=='('||c=='['){
			stk.push(c);
		}else if(c==')'||c==']'){
			if(stk.empty()){
				cout << "Wrong\n";
				return 0;
			}else{
				if(mp[stk.top()]==c) stk.pop();
				else{
					cout << "Wrong\n";
					return 0;
				}
			}
		}
	}
	cout << (stk.empty()?"OK\n":"Wrong\n");
	return 0;
}

问题 D: 数据结构-栈-字符串匹配

 

思路:

        这个题相比上一题,不仅要匹配的数量更多,并且还要注意括号优先级的问题,我这里选择再加了一个哈希表来匹配优先级。

参考题解:

#include<bits/stdc++.h>
using namespace std;
stack<char> stk;
string line;
int n;
unordered_map<char,char> mp = {{'<','>'},{'(',')'},{'[',']'},{'{','}'}};
unordered_map<char,int> pri = {{'<',1},{'(',2},{'[',3},{'{',4}};
void solve(){
	cin >> line;
	while(stk.size()) stk.pop();
	for(char c:line){
		if(c=='<'||c=='('||c=='['||c=='{'){
			if(stk.empty()) stk.push(c);
			else{
				if(pri[c]>pri[stk.top()]){
					cout << "NO\n";
					return;
				}else stk.push(c);
			}
		}else if(c=='>'||c==')'||c==']'||c=='}'){
			if(stk.empty()){
				cout << "NO\n";
				return;
			}else{
				if(mp[stk.top()]==c) stk.pop();
				else{
					cout << "NO\n";
					return;
				}
			}
		}
	}
	cout << (stk.empty()?"YES\n":"NO\n");
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin >> n;
	while(n--) solve();
	return 0;
}

问题 E: 数据结构-栈-计算

思路:

        利用中缀表达式可以轻松求解,注意符号之间的优先级问题,计算'-'、'/'、'^'时要注意参与运算的两个数字的先后顺序。数据范围会到2^32-1,要开long long,虽然这个题的后台测试点没有取到那么大,不过根据题意还是要这么做的。

参考题解:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
stack<char> op;
stack<ll> num;
unordered_map<char,int> pri={{'+',1},{'-',1},{'*',2},{'/',2},{'^',3}};
ll x;
string line;
bool flag;
void cal(){
	ll num1 = num.top();num.pop();
	ll num2 = num.top();num.pop();
	char op1 = op.top();op.pop();
	if(op1=='+') num.push(num1+num2);
	else if(op1=='-') num.push(num2-num1);
	else if(op1=='*') num.push(num1*num2);
	else if(op1=='/') num.push(num2/num1);
	else if(op1=='^') num.push(pow(num2,num1));
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin >> line;
	for(auto c:line){
		if(c>='0'&&c<='9'){
			x=x*10+c-'0';
			flag = true;
		}else{
			if(flag){
				num.push(x);
				flag = false;
				x = 0;
			} 
			if(c=='('){
				op.push(c);
			}else if(c==')'){
				while(op.top()!='('){
					cal();
				}
				op.pop();
			}else{
				while(op.size()&&pri[op.top()]>=pri[c]){
					cal();
				}
				op.push(c);
			}
		}
	}
	if(flag){
		num.push(x);
		x = 0;
		flag = false;
	}
	while(op.size()){
		cal();
	}
	cout << num.top() << '\n';
	return 0;
}

问题 F: 数据结构-栈-中缀表达式值

 

思路:

        跟上一题同理,但是这个题额外要注意的是可能存在负数,并且要判断该中缀表达式的合理性。

参考题解:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
stack<char> op;
stack<ll> num;
unordered_map<char,int> pri={{'+',1},{'-',1},{'*',2},{'/',2},{'^',3}};
ll x;
string line;
bool flag;
void cal(){
	ll num1 = num.top();num.pop();
	ll num2 = num.top();num.pop();
	char op1 = op.top();op.pop();
	if(op1=='+') num.push(num1+num2);
	else if(op1=='-') num.push(num2-num1);
	else if(op1=='*') num.push(num1*num2);
	else if(op1=='/') num.push(num2/num1);
	else if(op1=='^') num.push(pow(num2,num1));
}
void solve(){
	for(auto c:line){
		if(c>='0'&&c<='9'){
			x=x*10+c-'0';
			flag = true;
		}else{
			if(flag){
				num.push(x);
				flag = false;
				x = 0;
			}
			if(c=='@') break;
			else if(c=='('){
				op.push(c);
			}else if(c==')'){
				while(op.top()!='('){
					cal();
				}
				op.pop();
			}else{
				while(op.size()&&pri[op.top()]>=pri[c]){
					cal();
				}
				op.push(c);
			}
		}
	}
	if(flag){
		num.push(x);
		x = 0;
		flag = false;
	}
	while(op.size()){
		cal();
	}
	cout << num.top() << '\n';
}
bool check(){
	if(line.size()==2) return pri[line[0]]>0?false:true;
	for(int i = 1;i<line.size()-1;i++){
		if(pri[line[i]]&&pri[line[i-1]]) return false;
	}
	ll sum = 0;
	for(int i = 0;i<line.size()-1;i++){
		if(line[i]=='(') sum++;
		else if(line[i]==')') sum--;
	}
	return sum==0;
}
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	cin >> line;
	if(line[0]=='-') num.push(0);
	if(!check()) cout << "NO\n";
	else solve();
	return 0;
}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Beau_Will

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值