实验10:Problem C: STL——呵呵型自动机

指令POP id  t有误,只输出队首元素,而不是前t个。题目的问题,因为我Output Limit Exceed50%了7次。。。。。

Home Web Board ProblemSet Standing Status Statistics
 
Problem C: STL——呵呵型自动机

Problem C: STL——呵呵型自动机

Time Limit: 8 Sec  Memory Limit: 128 MB
Submit: 1890  Solved: 731
[Submit][Status][Web Board]

Description

xiaofei最近研发了一个呵呵型自动机,该自动机能够同时处理n个队列。其中,队列的编号为1..n。给定m个操作,模拟该自动机的工作状态。
第一行有2个整数n,m(1≤n, m≤10,000),表示自动机能处理n个队列,接下来m行每行一条操作指令。
每条指令的格式如下:
指令
指令说明
INIT
将自动机初始化,此时所有的队列清空。
PUSH id val t
把t个整数val加入到编号id的队列的尾部。
POP id  t
输出并删除编号id的队列的前t个队首元素,如果队列为空,则输出“NULL”。
 
在每条指令中,id的编号在1..n中,val的取值范围为-231~231。输入数据保证操作的第一条指令都是是INIT。

Input

本题有多组输入数据,你必须处理到EOF为止。

Output

请对输入数据中每条POP指令的结果依次输出一行结果。

Sample Input

3 12 INIT PUSH 1 100 1 POP 2 1 PUSH 3 300 1 PUSH 1 200 1 PUSH 2 -5 1 POP 2 1 PUSH 2 -10 1 POP 1 1 INIT PUSH 1 7 1 POP 1 1

Sample Output

NULL -5 100 7

HINT

用STL的queue容易解决

Append Code

 
[Submit][Status][Web Board]
#include<iostream>
#include<queue>
#include<string>
#define MAX 10002
using namespace std;

int main()
{
    queue<int>q[MAX];
    int n,m;
    string c;
    int id,val;
    while(cin>>n)
    {
        cin>>m;
        for(int i=0;i<m;i++)
        {

            cin>>c;
            if(c=="INIT")
            {
                for(int j=1;j<=m;j++)
                    while(!q[j].empty())
                        q[j].pop();
            }
            else if(c=="PUSH")
            {
                int t;

                cin>>id>>val>>t;
                while(t--)
                q[id].push(val);
            }
            else if(c=="POP")
            {
                int t;

                cin>>id>>t;


                    if(q[id].empty())
                    cout<<"NULL"<<endl;
                    else
                  {
                      queue<int> nq;
                    cout<<q[id].front()<<endl;
                    q[id]=nq;
                  }


            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/auto1945837845/p/5471494.html