您的位置:首页 > 产品设计 > UI/UE

UVA 11995 I Can Guess the Data Structure!

2015-06-22 14:10 477 查看
11995

I Can Guess the Data Structure!

There is a bag-like data structure, supporting two operations:

1 x Throw an element x into the bag.

2 Take out an element from the bag.

Given a sequence of operations with return values, you’re going to guess the data structure. It is

a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger

elements first) or something else that you can hardly imagine!

Input

There are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤

1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x.

That means after executing a type-2 command, we get an element x without error. The value of x is

always a positive integer not larger than 100. The input is terminated by end-of-file (EOF).

Output

For each test case, output one of the following:

stack It’s definitely a stack.

queue It’s definitely a queue.

priority queue It’s definitely a priority queue.

impossible It can’t be a stack, a queue or a priority queue.

not sure It can be more than one of the three data structures

mentioned above.

Sample Input

6

1 1

1 2

1 3

2 1

2 2

2 3

6

1 1

1 2

1 3

2 3

2 2

2 1

2

1 1

2 2

4Universidad de Valladolid OJ: 11995 – I Can Guess the Data Structure! 2/2

1 2

1 1

2 1

2 2

7

1 2

1 5

1 1

1 3

2 5

1 4

2 4

Sample Output

queue

not sure

impossible

stack

priority queue

/*
Author: 2486
Memory: 0 KB		    Time: 18 MS
Language: C++ 4.8.2		Result: Accepted

*/
//将所有的STL分别进行判断就可以了
#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
int n,cmd,dig;
bool isstack,isque,isprique;
stack<int>sta;
queue<int>que;
priority_queue<int>prique;
int main() {
#ifndef ONLINE_JUDGE
freopen("D://imput.txt","r",stdin);
#endif // ONLINE_JUDGE
while(~scanf("%d",&n)) {
while(!sta.empty())sta.pop();
while(!que.empty())que.pop();
while(!prique.empty())prique.pop();
isstack=true,isque=true,isprique=true;
for(int i=0; i<n; i++) {
scanf("%d%d",&cmd,&dig);
if(isstack) {
if(cmd==1) {
sta.push(dig);
} else {
if(sta.empty()) {
isstack=false;
while(!sta.empty())sta.pop();
} else {
int v=sta.top();
if(v==dig) {
sta.pop();
} else {
isstack=false;
while(!sta.empty())sta.pop();
}
}

}
}
if(isque) {
if(cmd==1) {
que.push(dig);
} else {
if(que.empty()) {
isque=false;
while(!que.empty())que.pop();
} else {
int v=que.front();
if(v==dig) {
que.pop();
} else {
isque=false;
while(!que.empty())que.pop();
}
}
}
}
if (isprique) {
if(cmd==1) {
prique.push(dig);
} else {
if(prique.empty()) {
isprique=false;
while(!prique.empty())prique.pop();
} else {
int v=prique.top();
if(v==dig) {
prique.pop();
} else {
isprique=false;
while(!prique.empty())prique.pop();
}
}
}
}
}
if(isstack+isque+isprique==1) {
if(isstack)printf("stack");
else if(isque)printf("queue");
else printf("priority queue");
} else if(isstack+isque+isprique==0) {
printf("impossible");
} else {
printf("not sure");
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: