Problem Statement :
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). The size of input file does not exceed 1MB.
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 4 1 2 1 1 2 1 2 2 7 1 2 1 5 1 1 1 3 2 5 1 4 2 4
Output for the Sample Input
queue not sure impossible stack priority queue
Solution :
#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main() {
int i,j,k,l,n,opt,tmp,temp;
while(scanf("%d",&n) == 1) {
stack S;
queue Q;
priority_queue P;
bool ss ,qq, pp;
ss = pp = qq = true;
while(n--) {
scanf("%d %d",&opt,&tmp);
if(opt == 1) {
if(ss)
S.push(tmp);
if(qq)
Q.push(tmp);
if(pp)
P.push(tmp);
}
else if(opt == 2) {
if(ss) {
if(!S.empty() && S.top() == tmp) {
S.pop();
}
else ss = false;
}
if(qq) {
if(!Q.empty() && Q.front() == tmp) {
Q.pop();
}
else
qq = false;
}
if(pp) {
if(!P.empty() && P.top() == tmp) {
P.pop();
}
else pp = false;
}
}
}
if(ss && !pp && !qq)
printf("stack\n");
else if(!ss && !pp && qq)
printf("queue\n");
else if(!ss && pp && !qq)
printf("priority queue\n");
else if(pp || qq || ss)
printf("not sure\n");
else
printf("impossible\n");
}
return 0;
}

2 comments:
#include
using namespace std;
int main() {
/*freopen("D:\\googleDrive\\_CSE\\_dayWise_work\\_Contest\\_iamfake\\_june\\uva_11995_I_Can_Guess_the_Data _Structure!\\in.txt","r",stdin);
freopen("D:\\googleDrive\\_CSE\\_dayWise_work\\_Contest\\_iamfake\\_june\\uva_11995_I_Can_Guess_the_Data _Structure!\\out.txt","w",stdout);
*/
int tc;
stack s;
queue q;
priority_queue pq;
while(scanf("%d",&tc)==1)
{
bool iss = true, isq = true, ispq = true;
//cout << "--> " << tc << endl;
for(int t=1;t<=tc;t++) {
int m, n;
scanf("%d%d", &m, &n);
//cout << m << " ----- " << n << endl;
if (m == 1) {
if(iss) s.push(n);
if(isq)q.push(n);
//cout << "pushed inside queue" << endl;
if(ispq)pq.push(n);
}
if (m == 2) {
//cout << s.size() << q.size() << pq.size() << endl;
if(!q.empty()) {
if (q.front() == n && isq) {
q.pop();
} else {
isq = false;
}
}
if(!s.empty()) {
if (s.top() == n && iss) {
s.pop();
} else {
iss = false;
}
}
if(!pq.empty()) {
if (pq.top() == n && ispq) {
pq.pop();
} else {
ispq = false;
}
}
}
}
//cout << "end of loop " << endl;
//if(iss && ispq)cout << "happening" << endl;
if(iss && !isq && !ispq)
{
cout <<"stack" << endl;
}
else if(isq && !iss && !ispq)
{
cout << "queue" << endl;
}
else if(ispq && !iss && !isq)
{
cout << "priority queue" << endl;
}
else if(iss || isq || ispq)
{
cout << "not sure" << endl;
}
else{
cout << "impossible" << endl;
}
while(!s.empty())s.pop();
while(!q.empty())q.pop();
while(!pq.empty())pq.pop();
}
return 0;
}
//why WA??
Post a Comment