思路
上面的轨道可以看作若干的队列,下面的筐可以看作栈,问题就简化成了栈和队列的操作。
代码
#include<iostream>
#include<queue>
#include<stack>
#include<string>
using namespace std;
queue<char> q[105];
stack<char> s;
string ans = "";
int main(){
int n, m, smax;
cin>>n>>m>>smax;
for(int i=1; i <= n; i++){
for(int j=0; j < m; j++){
char a;
cin>>a;
q[i].push(a);
}
}
while(1){
int opt;
cin>>opt;
if(opt == -1){
break;
}
if(opt){
if(s.size() == smax){
if(!q[opt].empty()){
ans += s.top();
s.pop();
char a = q[opt].front();
q[opt].pop();
s.push(a);
}
}else{
if(!q[opt].empty()){
char a = q[opt].front();
q[opt].pop();
s.push(a);
}
}
}else if(!s.empty()){
ans += s.top();
s.pop();
}
}
cout<<ans<<endl;
return 0;
}
本文解析了如何利用栈和队列数据结构解决实际问题,通过实例展示了在给定代码中栈与队列的交互操作,适合深入理解这两种数据结构在算法中的运用。
356

被折叠的 条评论
为什么被折叠?



