Problem D: STL——哈哈型自动机

xiaoxiao2021-02-28  67

HomeWeb BoardProblemSetStandingStatusStatistics

Problem D: STL——哈哈型自动机

Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1599  Solved: 1140 [ Submit][ Status][ Web Board]

Description

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

Input

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

Output

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

Sample Input

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

Sample Output

NULL-51007

HINT

STL中的queue容器。 关于队列的知识; 使用queue之前,要先利用构造函数一个队列对象,才可以进行元素的入队,出队,取队首和队尾等操作; (1).queue() queue<int> q; 或者 queue<int>Q[10000];  (2).queue(const queue&) 复制构造函数  例如:用一行代码利用queue对象q1,创建一个以双向链表为底层容器的queue对象q2 queue<int,list<int>>q1; queue<int,list<int>>q2(q1); (3).元素入队 函数为:push()例如:q.push(3),意思是将3入队 ,注意队列的大小不预设 (4).元素出队 函数为:pop()例如:q.pop() (5)。取对头元素  函数为:front() (6),取队尾元素  函数为:back() (7)。判断对空  函数为:empty() (8)。队列的大小  函数为:size()返回队列的当前元素的个数

Append Code

[ Submit][ Status][ Web Board]

한국어<   中文  فارسی  English  ไทย All Copyright Reserved 2010-2011 SDUSTOJ TEAM GPL2.0 2003-2011 HUSTOJ Project TEAM Anything about the Problems, Please Contact Admin:admin

#include <iostream> #include <queue> using namespace std; int main() { int m, n; while( cin >> n) { cin >> m; queue <int >Q[n]; for(int p = 0; p < m; p++ ) { string s; cin >> s; if(s == "INIT") for(int i = 0; i < n; i++) while(!Q[i].empty()) Q[i].pop(); if(s == "POP" ) { int i; cin >> i; if(Q[i].empty()) cout << "NULL" << endl; else { cout << Q[i].front() << endl; Q[i].pop(); } } if(s == "PUSH") { int i, j; cin >> i >> j; Q[i].push(j); } } } }
转载请注明原文地址: https://www.6miu.com/read-79739.html

最新回复(0)