SGU 271 好久不寫題,腦袋生鏽了 奇怪的隊列。。

可以逆置前 K 個書名。

操作比較多,一看就是數據結構的題目。

操作只涉及前 K 個,所以,我用一個棧來保存不會被操作的數據。需要被逆置或者增加的數據我使用一個可以交換隊頭隊尾的隊列來完成。

隊列的大小是 K ,如果滿了就從隊頭(最下面的那本書)裏拿出一個放入棧中。

逆序就是交換頭尾而已。

這樣就 OK 了。

#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <set>
#include <stack>
#include <deque>
#include <list>
using namespace std;

#define READ freopen("acm.in","r",stdin)
#define WRITE freopen("acm.out","w",stdout)
#define ll long long
#define ull unsigned long long 
#define uint unsigned int
#define PII pair<int,int>
#define PDD pair<double,double>
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 0x3f3f3f3f
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define MOD 99991
#define MAX 1000000


stack<string> S;
int n,m,k;

struct tmpQ
{
    string q[200000];
    int f,b;
    int state;

    tmpQ()
    {
        state=0;
        f=100000;
        b=f+1;
    }
    void rotate()
    {
        state=!state;
    }
    void push(string s)
    {
        if(state)
            q[f--]=s;
        else
            q[b++]=s;
        if(size()>k)
        {
            S.push(front());
            pop_front();
        }
    }

    bool empty()
    {
        return size()<=0;
    }

    string front()
    {
        if(state)
            return q[b-1];
        else
            return q[f+1];
    }

    void pop_front()
    {
        if(state)
            b--;
        else
            f++;       
    }

    int size()
    {
        return b-f-1;
    }
}Q;

int main()
{
    //freopen("acm.in","r",stdin);
    scanf("%d%d%d",&n,&m,&k);
    stack<string> tmpS;
    for(int i=0;i<n;i++)
    {
        char c[123];
        scanf("%s",c);
        string ss(c);
        tmpS.push(ss);
    }

    while(!tmpS.empty())
        Q.push(tmpS.top()),tmpS.pop();

    for(int i=0;i<m;i++)
    {
        char c[1231];
        scanf("%s",c);
        if(c[0]=='A')
        {
            string s;
            bool flag=false;
            for(int i=0;c[i]!=')';i++)
            {
                if(flag)
                    s+=c[i];
                if(c[i]=='(')
                    flag=true;
            }
            Q.push(s);
        }
        else
        {
            Q.rotate();
        }
    }
    while(!Q.empty())
    {
        S.push(Q.front());
        Q.pop_front();
    }
    while(!S.empty())
        cout<<S.top()<<endl,S.pop();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章