欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【STL队列和栈】HDU-1702 ACboy needs your help again!

程序员文章站 2022-07-16 09:50:36
...

【STL队列和栈】HDU-1702 ACboy needs your help again!
【STL队列和栈】HDU-1702 ACboy needs your help again!

注解

1、熟练使用STL中的栈和队列。

代码

#include <iostream>
#include <queue>
#include <stack>

using namespace std;

int main() {

    int T;
    cin>>T;
    
    for(int i=0; i<T; i++) {
        int n;
        cin>>n;
        string s;
        cin>>s;
        if(s.compare("FILO")) {
            queue<int> q;
            for(int j=0; j<n; j++) {
                string str;
                cin>>str;
                if(str.compare("IN")) {
                    if(q.size()>0) {
                        int ans = q.front();
                        q.pop();
                        cout<<ans<<endl;
                    } else {
                        cout<<"None"<<endl;
                    }
                } else {
                    int tmp;
                    cin>>tmp;
                    q.push(tmp);
                }
            }
        } else {
            stack<int> st;
            for(int j=0; j<n; j++) {
                string str;
                cin>>str;
                if(str.compare("IN")) {
                    if(st.size()>0) {
                        int ans = st.top();
                        st.pop();
                        cout<<ans<<endl;
                    } else {
                        cout<<"None"<<endl;
                    }
                } else {
                    int tmp;
                    cin>>tmp;
                    st.push(tmp);
                }
            }
        }
    }

    return 0;
}

结果

【STL队列和栈】HDU-1702 ACboy needs your help again!

相关标签: hdu STL队列和栈