栈和队列上手题 HDU1702 HDU1022 HDU1237 HDU3328 HDU1873 HDU1509 HDU1870 HDU1387 题解

1. HDU1702 ACboy needs your help again!

#include <iostream>
#include <string>
#include <stack>
#include <queue>
using namespace std;

int main(){
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while(T--){
        int n;
        string str;
        cin >> n >> str;
        if(str.compare("FIFO") == 0){
            queue<int> q;
            while(n--){
                string s;
                cin >> s;
                int num;
                if(s.compare("IN") == 0){
                    cin >> num;
                    q.push(num);
                }
                else{
                    if(q.empty()){
                        cout << "None" << endl;
                        continue;
                    }
                    cout << q.front() << endl;
                    q.pop();
                }
            }
        }
        else{
            stack<int> stack;
            while(n--){
                string s;
                cin >> s;
                int num;
                if(s.compare("IN") == 0){
                    cin >> num;
                    stack.push(num);
                }
                else{
                    if(stack.empty()){
                        cout << "None" << endl;
                        continue;
                    }
                    cout << stack.top() << endl;
                    stack.pop();
                }
            }
        }
    }

}


2. HDU1022 Train Problem I

#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>
#include <string>
using namespace std;
int main(){
    int n;
    while(~scanf("%d", &n)){
        string a, b;
        string str = "";
        cin >> a >> b;
        
        stack<char> s;
        string::iterator it_a, it_b;
        for(it_a = a.begin(), it_b = b.begin(); it_b != b.end() && it_a != a.end();){
            if(s.empty() || s.top() != *it_b){
                s.push(*it_a++);
                str += "in\n";
            }
            else{
                s.pop();
                ++it_b;
                str += "out\n";
            }
        }
        //栈有可能没pop完(因为如果b没遍历完且栈顶元素与当前遍历位置的数组元素相同,则需要pop),如果for循环里面不写it_a != a.end(),则会数组越界
        while(it_b != b.end() && s.top() == *it_b){
            s.pop();
            ++it_b;
            str += "out\n";
        }

        if(s.empty())
            cout << "Yes." << endl << str;
        else
            cout << "No." << endl;
        cout << "FINISH" << endl;
    }
    return 0;
}

3. HDU1237 简单计算器

#include <iostream>
#include <cstdio>
#include <stack>
#include <string>
using namespace std;
int main(){
    double n;
    while(~scanf("%lf", &n)){
        if(getchar() == '\n' && n == 0)
            break;
        stack<double> s;
        s.push(n);
        char ch;
        ch = getchar();
        while(scanf("%lf", &n)){
            if(ch == '+')
                s.push(n);
            else if(ch == '-')
                s.push(-n);
            else if(ch == '*'){
                double num = s.top() * n;
                s.pop();
                s.push(num);
            }
            else if(ch == '/'){
                double num = s.top() / n;
                s.pop();
                s.push(num);
            }
            if(getchar() == '\n')
                break;
            ch = getchar();
        }
        double sum = 0;
        while(!s.empty()){
            sum += s.top();
            s.pop();
        }
       printf("%0.2lf\n", sum);
    }
}

4. HDU3328 Flipper

#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main(){
    int T;
    int Case = 0;
    while(cin >> T && T){
        string order, perform;
        cin >> order >> perform;
        bool face[107];//记录纸牌的朝向,制定朝上Up为true,false为朝下Down
        stack<int> s[107];
        int cnt = 1;
        for(string::iterator it = order.begin(); it != order.end(); ++it, ++cnt){
            if(*it == 'U'){
                face[cnt] = true;
                s[cnt].push(cnt);
            }
            else{
                face[cnt] = false;
                s[cnt].push(cnt);
            }
        }

        for(string::iterator it = perform.begin(); it != perform.end(); ++it){
            if(*it == 'R'){
                //从右数起第一个非空的栈,对其进行反转,然后push到其左边的栈中
                int i;
                for(i = T; i > 0; i--)
                    if(!s[i].empty())
                        break;
                if(i == 1)
                    break;
                else{
                    while(!s[i].empty()){
                        face[s[i].top()] = !face[s[i].top()];
                        s[i - 1].push(s[i].top());
                        s[i].pop();
                    }
                }
            }
            else{
                //从左数起第一个非空的栈,对其进行反转,然后push到其右边的栈中
                int i;
                for(i = 1; i <= T; i++)
                    if(!s[i].empty())
                        break;
                if(i == T)
                    break;
                else{
                    while(!s[i].empty()){
                        face[s[i].top()] = !face[s[i].top()];
                        s[i + 1].push(s[i].top());
                        s[i].pop();
                    }
                }
            }
        }
        int p[107];
        cnt = 1;
        for(int i = 1; i <= T; i++)
            if(!s[i].empty()){
                while(!s[i].empty()){
                    p[cnt++] = s[i].top();
                    s[i].pop();
                }
                break;
            }
        cout << "Pile " << ++Case << endl;
        int n;
        cin >> n;
        for(int i = 0; i < n; i++){
            int t;
            cin >> t;
            cout << "Card " << t << " is a face ";
            if(face[p[t]])
                cout << "up " << p[t] << ".\n";
            else
                cout << "down " << p[t] << ".\n";
        }
    }
}

5. HDU1873  看病要排队

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

struct Node{
    int id, priority;
    friend bool operator < (const Node a, const Node b){
        if(a.priority == b.priority)
            return a.id > b.id;
        return a.priority < b.priority;
    }
} node;

int main(){
    cin.tie(0);
    cout.tie(0);
    int n;
    while(cin >> n){
        priority_queue<Node> q[5];
        int cnt = 0;
        while(n--){
            string order;
            cin >> order;
            int A;
            if(order.compare("IN") == 0){
                node.id = ++cnt;
                cin >> A >> node.priority;
                q[A - 1].push(node);
            }
            else{
                cin >> A;
                if(q[A - 1].empty())
                    cout << "EMPTY" << endl;
                else{
                    cout << q[A - 1].top().id << endl;
                    q[A - 1].pop();
                }
            }
        }
    }
}

6. HDU1509  Windows Message Queue

#include <iostream>
#include <queue>
using namespace std;
struct Node{
    //另设id方便记录序号
    int id;
    string msg;
    int param, priority;
    friend bool operator < (const Node a, const Node b){
        if(a.priority == b.priority)
            return a.id > b.id;
        return a.priority > b.priority;
    }
} node;

int main(){
    cin.tie(0);
    cout.tie(0);
    priority_queue<Node> q;
    string command;
    int ID = 0;
    while(cin >> command){
        if(command == "GET"){
            if(q.empty())
                cout << "EMPTY QUEUE!" << endl;
            else{
                cout << q.top().msg << ' ' << q.top().param << endl;
                q.pop();
            }
        }
        else{
            ++ID;
            node.id = ID;
            cin >> node.msg >> node.param >> node.priority;
            q.push(node);
        }
    }
}

7. HDU1870  愚人节的礼物

#include <iostream>
#include <queue>
using namespace std;
int main(){
    cin.tie(0);
    cout.tie(0);
    queue<char> q;
    string str;
    while(cin >> str){
        for(string::iterator it = str.begin(); it != str.end(); it++){
            if(*it == 'B'){
                int ans = 0;
                while(!q.empty()){
                    q.pop();
                    ans++;
                }
                cout << ans << endl;
                break;
            }
            else if(*it == '(')
                q.push('(');
            else if(*it == ')')
                q.pop();
        }
    }
}

8. HDU1387  Team Queue

#include <iostream>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int MAXN = 1007;
typedef struct{
    int id, num;
} Node;
set<int> team[MAXN];
int main(){
    cin.tie(0);
    cout.tie(0);
    int T;
    int Case = 0;
    while(cin >> T && T){
        int n;
        for(int i = 0; i < T; i++){
            cin >> n;
            int tmp;
            while(n--){
                cin >> tmp;
                team[i].insert(tmp);
            }
        }
        cout << "Scenario #" << ++Case << endl;
        queue<Node> q[MAXN];
        string command;
        int elem;
        int team_cnt = 0;
        while(cin >> command && (command != "STOP")){
            if(command == "DEQUEUE"){
                //第一个非空队列的第一个元素出队
                for(int i = 0; i < team_cnt; i++){
                    if(!q[i].empty()){
                        cout << q[i].front().num << endl;
                        q[i].pop();
                        break;
                    }
                }
            }
            else{
                Node node;
                cin >> elem;
                int team_num;
                set<int>::iterator it;
                //找输入元素所属的队伍编号
                for(int i = 0; i < T; i++){
                    it = team[i].find(elem);
                    if(it != team[i].end()){
                        team_num = i;
                        break;
                    }
                }
                node.id = team_num, node.num = elem;
                bool hasSame = false;
                for(int i = 0; i < team_cnt; i++)
                    if(!q[i].empty() && q[i].front().id == team_num){
                        q[i].push(node);
                        hasSame = true;
                        break;
                    }
                //该队伍没有人在队列中
                if(!hasSame)
                    q[team_cnt++].push(node);
            }
        }
        cout << endl;
    }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值