双向队列sdut 1466

双向队列

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

      想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首;两头都可以做出队,入队的操作。
现在给你一系列的操作,请输出最后队列的状态;
命令格式:
LIN X  X表示一个整数,命令代表左边进队操作;
RIN X  表示右边进队操作;
ROUT
LOUT   表示出队操作;

Input

第一行包含一个整数M(M<=10000),表示有M个操作;
以下M行每行包含一条命令;
命令可能不合法,对于不合法的命令,请在输出中处理;

Output

输出的第一行包含队列进行了M次操作后的状态,从左往右输出,每两个之间用空格隔开;
以下若干行处理不合法的命令(如果存在);
对于不合法的命令,请输出一行X ERROR
其中X表示是第几条命令;

Sample Input

8
LIN 5
RIN 6
LIN 3
LOUT
ROUT
ROUT
ROUT
LIN 3

Sample Output

3
7 ERROR

方法1:

#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    scanf("%d",&n);
    char a[100];
    int num[100100];
    int qwe[10010];
    int e1=10050;
    int e2=10050;
    int flog=0;
    int p;
 
    for(int i=1; i<=n; i++)
    {
        scanf("%s",a);
        if(strcmp(a,"LIN")==0)
        {
            scanf("%d",&p);
            num[e1--]=p;
        }
        else if(strcmp(a,"RIN")==0)
        {
            scanf("%d",&p);
            num[++e2]=p;
        }
        else if(strcmp(a,"LOUT")==0)
        {
            if(e1>=e2)//说明没有数了
            {
                qwe[flog++]=i;//出现不满足的情况
                e1=10050;//恢复原值
                e2=10050;//恢复原值
            }
            else
            {
                e1++;
            }
        }
        else
        {
            if(strcmp(a,"ROUT")==0)
            {
                if(e1>=e2)
                {
                    qwe[flog++]=i;
                    e1=10050;
                    e2=10050;
                }
                else
                {
                    e2--;
                }
            }
        }
    }
 
 
        for(int i=e1+1; i<=e2; i++)
        {
            if(i==e2)
                printf("%d\n",num[i]);
            else
            {
                printf("%d ",num[i]);
            }
        }
 
    for(int i=0; i<flog; i++)
    {
        printf("%d ERROR\n",qwe[i]);
    }
    return 0;
}


方法2(用数组模拟):

#include<iostream>
#include<algorithm>
#include<string>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>


using namespace std;


int a[1000100];
int b[100010];
int n;


int main()
{
    while(cin >> n)
    {
        string str;
        int x;
        int s = 15000;
        int e = 15000;
        int h = 0;
        for(int i=1; i<=n; i++)
        {
            cin >> str;
            if(str == "LIN")
            {
                cin >> x;
                a[s--] = x;
            }
            else if(str == "RIN")
            {
                cin >> x;
                a[++e] = x;
            }
            else if(str == "LOUT")
            {
                if(s>=e)
                {
                    b[h++] = i;
                    s = 15000;
                    e = 15000;
                }
                else
                {
                    s++;
                }
            }
            else
            {
                if(str == "ROUT")
                {
                    if(s >=e)
                    {
                        b[h++] = i;
                        s = 15000;
                        e = 15000;
                    }
                    else
                    {
                        e--;
                    }
                }
            }
        }
        if(s < e)
        {
            for(int i=s+1; i<=e; i++)
            {
                if(i == e)
                {
                    printf("%d\n",a[i]);
                }
                else
                {
                    printf("%d ",a[i]);
                }
            }
        }
        for(int i=0; i<h; i++)
        {
            printf("%d ERROR\n",b[i]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Allinone99/article/details/81367095