Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Tuesday, December 18, 2012

uva 11995 - I Can Guess the Data Structure!


/*
Problem link
Type: Data structure
*/  
  1  #include <iostream>
  2  #include <cstdio>
  3  #include <cstring>
  4  #include <cmath>
  5  #include <cstdlib>
  6  #include <vector>
  7  #include <queue>
  8  #include <stack>
  9  
 10  using namespace std;
 11  const int maxn = 1010;
 12  
 13  class action{
 14  public:
 15      int type;
 16      int value;
 17  };
 18  
 19  stack<int> st;
 20  queue<int> q;
 21  priority_queue<int, vector<int>, less<int> > pq;
 22  int n;
 23  action list[maxn];
 24  
 25  bool checkst();
 26  bool checkq();
 27  bool checkpq();
 28  
 29  int main()
 30  {
 31      bool isst, isq, ispq;
 32      while (!cin.eof())
 33      {
 34          cin >> n;
 35          if (cin.eof()) break;
 36          while (!q.empty()) q.pop();
 37          while (!st.empty()) st.pop();
 38          while (!pq.empty()) pq.pop();
 39          for (int i=1; i<=n; i++) cin >> list[i].type >> list[i].value;
 40          isst = checkst();
 41          isq = checkq();
 42          ispq = checkpq();
 43          if (isst+isq+ispq>1) cout << "not sure" << endl;
 44          else if (isst+isq+ispq==0) cout << "impossible" << endl;
 45          else
 46          {
 47              if (isst) cout << "stack" << endl;
 48              else if (isq) cout << "queue" << endl;
 49              else if (ispq) cout << "priority queue" << endl;
 50          }
 51      }
 52      return 0;
 53  }
 54  
 55  bool checkst() {
 56      for (int i=1; i<=n; i++)
 57      {
 58          if (list[i].type==1) st.push(list[i].value);
 59          else if (list[i].type==2)
 60          {
 61              int tmp;
 62              if (!st.empty())
 63              {
 64                  tmp = st.top();
 65                  st.pop();
 66              }
 67              else return(false);
 68              if (tmp!=list[i].value) return(false);
 69          }
 70      }
 71      return(true);
 72  }
 73  
 74  bool checkq() {
 75      for (int i=1; i<=n; i++)
 76      {
 77          if (list[i].type==1) q.push(list[i].value);
 78          else if (list[i].type==2)
 79          {
 80              int tmp;
 81              if (!q.empty())
 82              {
 83                  tmp = q.front();
 84                  q.pop();
 85              }
 86              else return(false);
 87              if (tmp!=list[i].value) return(false);
 88          }
 89      }
 90      return(true);
 91  }
 92  
 93  bool checkpq() {
 94      for (int i=1; i<=n; i++)
 95      {
 96          if (list[i].type==1) pq.push(list[i].value);
 97          else if (list[i].type==2)
 98          {
 99              int tmp;
100              if (!pq.empty())
101              {
102                  tmp = pq.top();
103                  pq.pop();
104              }
105              else return(false);
106              if (tmp!=list[i].value) return(false);
107          }
108      }
109      return(true);
110  }

No comments:

Post a Comment