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;
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());
}
int main(){
m['+']=1,m['-']=1,m['*']=2,m['/']=2;
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}