标签:
题意:给你n个操做,判断是那种数据结构。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<stack> 5 #include<queue> 6 using namespace std; 7 int n; 8 int v[1010],u[1010]; 9 10 int ck_q() 11 { 12 //cout<<"!!"<<endl; 13 queue<int>s; 14 for(int i=0; i<n; i++) 15 { 16 if(v[i]==2) 17 { 18 if(s.empty()) 19 return 0; 20 int x=s.front(); 21 s.pop(); 22 if(x!=u[i]) 23 return 0; 24 } 25 else 26 s.push(u[i]); 27 } 28 return 1; 29 } 30 31 int ck_s() 32 { 33 stack<int>s; 34 for(int i=0; i<n; i++) 35 { 36 if(v[i]==2) 37 { 38 if(s.empty()) 39 return 0; 40 int x=s.top(); 41 s.pop(); 42 if(x!=u[i]) 43 return 0; 44 } 45 else 46 s.push(u[i]); 47 } 48 return 1; 49 } 50 51 int ck_d() 52 { 53 priority_queue<int>s; 54 for(int i=0; i<n; i++) 55 { 56 if(v[i]==2) 57 { 58 if(s.empty()) 59 return 0; 60 int x=s.top(); 61 s.pop(); 62 if(x!=u[i]) 63 return 0; 64 } 65 else 66 s.push(u[i]); 67 } 68 return 1; 69 } 70 int main() 71 { 72 while(~scanf("%d",&n)) 73 { 74 for(int i=0; i<n; i++) 75 { 76 scanf("%d%d",&v[i],&u[i]); 77 } 78 int s=ck_s(); 79 int q=ck_q(); 80 int d=ck_d(); 81 //cout<<s<<" "<<q<<" "<<d<<endl; 82 if(s&&!q&&!d) 83 printf("stack\n"); 84 else if(!s&&q&&!d) 85 printf("queue\n"); 86 else if(!s&&!q&&d) 87 printf("priority queue\n"); 88 else if(!s&&!q&&!d) 89 printf("impossible\n"); 90 else 91 printf("not sure\n"); 92 } 93 return 0; 94 }
uva 11995 I Can Guess the Data Structure stack,queue,priority_queue
标签:
原文地址:http://www.cnblogs.com/ITUPC/p/5074422.html