UVA 11995

來源:互聯網
上載者:User

標籤:style   blog   color   資料   os   cti   

I Can Guess the Data Structure!

Time limit: 1.000 seconds

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
61 11 21 32 12 22 361 11 21 32 32 22 121 12 241 21 12 12 271 21 51 11 32 51 42 4
Output for the Sample Input
queuenot sureimpossiblestackpriority queue

 考查基本資料結構定義,實現判斷即可。

/** @author  Panoss*/#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<vector>#include<ctime>#include<stack>#include<queue>#include<list>using namespace std;#define DBG 0#define fori(i,a,b) for(int i = (a); i < (b); i++)#define forie(i,a,b) for(int i = (a); i <= (b); i++)#define ford(i,a,b) for(int i = (a); i > (b); i--)#define forde(i,a,b) for(int i = (a); i >= (b); i--)#define forls(i,a,b,n) for(int i = (a); i != (b); i = n[i])#define mset(a,v) memset(a, v, sizeof(a))#define mcpy(a,b) memcpy(a, b, sizeof(a))#define dout  DBG && cerr << __LINE__ << " >>| "#define checkv(x) dout << #x"=" << (x) << " | "<<endl#define checka(array,a,b) if(DBG) { \    dout << #array"[] | " << endl;     forie(i, a, b) cerr << "[" << i << "]=" << array[i] << " |" << ((i - (a)+1) % 5 ? " " : "\n"); if (((b)-(a)+1) % 5) cerr << endl; }#define redata(T, x) T x; cin >> x#define MIN_LD -2147483648#define MAX_LD  2147483647#define MIN_LLD -9223372036854775808#define MAX_LLD  9223372036854775807#define MAX_INF 18446744073709551615inline int  reint() { int d; scanf("%d", &d); return d; }inline long relong() { long l; scanf("%ld", &l); return l; }inline char rechar() { scanf(" "); return getchar(); }inline double redouble() { double d; scanf("%lf", &d); return d; }inline string restring() { string s; cin >> s; return s; }struct Cmd{    int cmd;    int x;};vector<Cmd> C;bool is_stack(){    stack<int> S;    fori(i,0,C.size())    {        if(C[i].cmd == 1)            S.push(C[i].x);        else        {            if(S.empty()) return false;            int value = S.top();            S.pop();            if(value != C[i].x) return false;        }    }    return true;}bool is_queue(){    queue<int> Q;    fori(i,0,C.size())    {        if(C[i].cmd == 1)            Q.push(C[i].x);        else        {            if(Q.empty()) return false;            int value = Q.front();            Q.pop();            if(value != C[i].x) return false;        }    }    return true;}bool is_pri_queue(){    priority_queue<int> Q;    fori(i,0,C.size())    {        if(C[i].cmd == 1)            Q.push(C[i].x);        else        {            if(Q.empty()) return false;            int value = Q.top();            Q.pop();            if(value != C[i].x) return false;        }    }    return true;}int main(){    int n;    while(scanf("%d",&n)==1)    {        C.clear();        forie(i,1,n)        {            Cmd command;            scanf("%d%d",&command.cmd,&command.x);            C.push_back(command);        }        bool is_st = is_stack();        bool is_qu = is_queue();        bool is_pri_qu = is_pri_queue();        if(!is_st&&!is_qu&&!is_pri_qu) puts("impossible");        else if(is_st&&is_qu&&is_pri_qu) puts("not sure");        else if((is_st&&is_qu&&!is_pri_qu)||(is_st&&!is_qu&&is_pri_qu)||(!is_st&&is_qu&&is_pri_qu)) puts("not sure");        else if(is_st&&!is_qu&&!is_pri_qu) puts("stack");        else if(!is_st&&is_qu&&!is_pri_qu) puts("queue");        else if(!is_st&&!is_qu&&is_pri_qu) puts("priority queue");    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.