【数据结构】栈

A. DS堆栈–逆序输出(STL栈使用)

#include<iostream>
#include<stack>

using namespace std;

int main(){
	int t;
	cin>>t;
	while(t--){
		stack<char>stk;
		string s;
		cin>>s;
		for(auto t:s)
			stk.push(t);
		while(stk.size()){
			cout<<stk.top();
			stk.pop();
		}
		cout<<endl;
	}
  return 0;
}

B. DS堆栈–行编辑

#include<iostream>
#include<stack>

using namespace std;

int main(){
	int t;
	cin>>t;
	while(t--){
		stack<char>stk;
		stack<char>ans;
		string s;
		cin>>s;
		for(auto t:s){
			if(t=='#'&&stk.size())
				stk.pop();
			else if(t!='#')	stk.push(t);
		}
		if(stk.size()){
			while(stk.size()){
				ans.push(stk.top());
				stk.pop();
			}
			while(ans.size()){
				cout<<ans.top();
				ans.pop();
			}
			cout<<endl;
		}
		else	puts("NULL");
	}
	return 0;
}

C. DS堆栈–迷宫求解

#include<bits/stdc++.h>
using namespace std;
int n;
int g[1000][1000];
bool st[1000][1000];
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
stack<pair<int,int>>stk;
bool succ;
void dfs(int x,int y){
	if(succ)	return;
	if(x==n-1&&y==n-1){
		stack<pair<int,int>>ans;
		while(stk.size()){
			ans.push(stk.top());
			stk.pop();
		}
		int cnt=0;
		while(ans.size()){
			auto t=ans.top();
			cout<<"["<<t.first<<","<<t.second<<"]--";
			if(cnt++%4==3)	cout<<endl;
			ans.pop();
		}
		succ=true;
		cout<<"END"<<endl;
	}
	for(int i=0;i<4;i++){
		int tx=x+dx[i],ty=y+dy[i];
		if(tx<0||ty<0||tx>=n||ty>=n)	continue;
		if(g[tx][ty]||st[tx][ty])	continue;
		st[tx][ty]=true;
		stk.push({tx,ty});
		dfs(tx,ty);
		if(succ)	break;
		stk.pop();
		st[tx][ty]=false;
	} 
}

void solve(){
	memset(st,false,sizeof st);
	succ=false;
	cin>>n;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			cin>>g[i][j];
	st[0][0]=true;
	stk.push({0,0});
	dfs(0,0);
	if(!succ)	puts("no path");
}

int main(){
	int t;
	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

D. DS堆栈–括号匹配

#include<iostream>
#include<stack>

using namespace std;

int main(){
	int t;
	cin>>t;
	while(t--){
		stack<char>stk;
		bool flag=true;
		string s;
		cin>>s;
		for(auto t:s){
			if(t=='('||t=='['||t=='{'){
				stk.push(t);
			}
			else if(t==')'||t==']'||t=='}'){
				if(stk.empty()){
					flag=false;
					break;
				}
				if((stk.top()!='('&&t==')')||(stk.top()!='{'&&t=='}')&&(stk.top()!='['&&t==']')){
					flag=false;
					break;
				}
				if(stk.size()) stk.pop();
			}
		}
		if(flag&&stk.empty())	puts("ok");
		else	puts("error");
	}
	return 0;
}

E. DS堆栈–表达式计算

#include<bits/stdc++.h>
using namespace std;

stack<double>num;
stack<char>op;
unordered_map<char,int>m;

void eval(){
	double b=num.top();num.pop();
	double a=num.top();num.pop();
	char OP=op.top();op.pop();
	double res;
	if(OP=='+')	res=a+b;
	else if(OP=='-')	res=a-b;
	else if(OP=='*')	res=a*b;
	else	res=a/b;
//	cout<<res<<endl;
	num.push(res);
}

void solve(){
	while(num.size())	num.pop();
	while(op.size())	op.pop();
	string s;
	cin>>s;
	for(int i=0;i<s.size()-1;i++){
		if(isdigit(s[i])){
			double res=0,j=i;
			while(j<s.size()&&isdigit(s[j]))
				res=res*10+s[j++]-'0';
			if(s[j]=='.'){
				j++;
				int cnt=-1;
				while(j<s.size()&&isdigit(s[j]))
					res+=(s[j++]-'0')*pow(10,cnt--);
			}
			i=j-1;
			num.push(res);
		}
		else if(s[i]=='(')	op.push(s[i]);
		else if(s[i]==')'){
			while(op.top()!='(')	eval();
			op.pop();
		}
		else{
			while(op.size()&&m[op.top()]>=m[s[i]])	eval();
			op.push(s[i]);
		}
	}
	while(op.size())	eval();
	printf("%.4lf\n",num.top());
//	cout<<num.top()<<endl;
}

int main(){
	m['+']=1,m['-']=1,m['*']=2,m['/']=2;
	int t;
	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值