日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uva 11995 I Can Guess the Data Structure!

發布時間:2024/6/30 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uva 11995 I Can Guess the Data Structure! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

https://vjudge.net/problem/UVA-11995

題意:

現在有一個未知的數據結構,給出n個操作,如果說操作為1的話,給出一個x放入這個結構,如果說操作為2的話,表示從里面拿出來了一個數x。

現在問是否有一種數據結構符合這組數據的操作,給定的數據結構有棧,隊列,優先隊列。

思路:

由于有了現成的stl,那么直接模擬算算是否符合就行了,注意容器要判斷容器是否為空。

代碼:

1 #include <stdio.h> 2 #include <stack> 3 #include <queue> 4 using namespace std; 5 6 stack<int> s; 7 queue<int> q; 8 priority_queue<int> pq; 9 10 struct node 11 { 12 int op; 13 int x; 14 } a[1005]; 15 16 int main() 17 { 18 int n; 19 20 while (scanf("%d",&n) != EOF) 21 { 22 while (!s.empty()) s.pop(); 23 while (!q.empty()) q.pop(); 24 while (!pq.empty()) pq.pop(); 25 26 for (int i = 0;i < n;i++) 27 { 28 scanf("%d%d",&a[i].op,&a[i].x); 29 } 30 31 bool f = 0,ff = 0,fff = 0; 32 33 for (int i = 0;i < n;i++) 34 { 35 if (a[i].op == 1) 36 { 37 s.push(a[i].x); 38 } 39 else 40 { 41 int tmp; 42 43 if (s.empty()) 44 { 45 f = 1;break; 46 } 47 else if (!s.empty()) tmp = s.top(),s.pop(); 48 49 if (tmp != a[i].x) 50 { 51 f = 1;break; 52 } 53 } 54 } 55 56 for (int i = 0;i < n;i++) 57 { 58 if (a[i].op == 1) 59 { 60 q.push(a[i].x); 61 } 62 else 63 { 64 if (q.empty()) 65 { 66 ff = 1;break; 67 } 68 else if (!q.empty()) 69 { 70 int tmp = q.front();q.pop(); 71 72 if (tmp != a[i].x) 73 { 74 ff = 1;break; 75 } 76 } 77 } 78 } 79 80 for (int i = 0;i < n;i++) 81 { 82 if (a[i].op == 1) pq.push(a[i].x); 83 else 84 { 85 if (pq.empty()) 86 { 87 fff = 1;break; 88 } 89 else if (!pq.empty()) 90 { 91 int tmp = pq.top();pq.pop(); 92 93 if (tmp != a[i].x) 94 { 95 fff = 1;break; 96 } 97 } 98 } 99 } 100 101 int cnt = 0; 102 103 if (f) cnt++; 104 if (ff) cnt++; 105 if (fff) cnt++; 106 107 if (cnt == 3) printf("impossible\n"); 108 else if (cnt <= 1) printf("not sure\n"); 109 else 110 { 111 if (!f) printf("stack\n"); 112 if (!ff) printf("queue\n"); 113 if (!fff) printf("priority queue\n"); 114 } 115 } 116 117 118 119 return 0; 120 }

?

轉載于:https://www.cnblogs.com/kickit/p/7635938.html

總結

以上是生活随笔為你收集整理的uva 11995 I Can Guess the Data Structure!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。