SGU 271 好久不写题,脑袋生锈了 奇怪的队列。。

来源:互联网 发布:手机淘宝 装修日记 编辑:程序博客网 时间:2024/04/28 00:31

可以逆置前 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 1000000stack<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;}


0 0