码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 11995 I Can Guess the Data Structure!

时间:2015-03-15 16:46:00      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

做道水题凑凑题量,=_=||。

直接用STL里的queue、stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空。

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 void scan( int &x )
 5 {
 6     char c;
 7     while( c = getchar(), c < 0 || c > 9 );
 8     x = c - 0;
 9     while( c = getchar(), c >= 0 && c <= 9 ) x = x*10 + c - 0;
10 }
11 
12 const int maxn = 1000 + 10;
13 int t[maxn], d[maxn];
14 
15 bool is_queue(int n)
16 {
17     queue<int> Q;
18     for(int i = 0; i < n; i++)
19     {
20         if(t[i] == 1) Q.push(d[i]);
21         else
22         {
23             if(Q.empty()) return false;
24             int x = Q.front(); Q.pop();
25             if(x != d[i]) return false;
26         }
27     }
28     return true;
29 }
30 
31 bool is_stack(int n)
32 {
33     stack<int> S;
34     for(int i = 0; i < n; i++)
35     {
36         if(t[i] == 1) S.push(d[i]);
37         else
38         {
39             if(S.empty()) return false;
40             int x = S.top(); S.pop();
41             if(x != d[i]) return false;
42         }
43     }
44     return true;
45 }
46 
47 bool is_p_queue(int n)
48 {
49     priority_queue<int> Q;
50     for(int i = 0; i < n; i++)
51     {
52         if(t[i] == 1) Q.push(d[i]);
53         else
54         {
55             if(Q.empty()) return false;
56             int x = Q.top(); Q.pop();
57             if(x != d[i]) return false;
58         }
59     }
60     return true;
61 }
62 
63 int main()
64 {
65     //freopen("in.txt", "r", stdin);
66 
67     int n;
68     while(scanf("%d", &n) == 1)
69     {
70         for(int i = 0; i < n; i++) { scan(t[i]); scan(d[i]); }
71         bool f1 = is_queue(n);
72         bool f2 = is_stack(n);
73         bool f3 = is_p_queue(n);
74         int cnt = (int)f1 + (int)f2 + (int)f3;
75         if(cnt > 1) puts("not sure");
76         else if(cnt == 0) puts("impossible");
77         else
78         {
79             if(f1) puts("queue");
80             if(f2) puts("stack");
81             if(f3) puts("priority queue");
82         }
83     }
84 
85     return 0;
86 }
代码君

 

UVa 11995 I Can Guess the Data Structure!

标签:

原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/4339911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!