Advertisement
Ermolaxe

Queue #5

Jun 30th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. #include <vector>
  4. #include <string>
  5. using namespace std;  
  6.  
  7. ofstream fout ("output.txt");
  8.  
  9. vector <queue <int> > vec;
  10.  
  11.  int Function (string func){
  12.     if (func[0]=='P'){
  13.         if (func[1]=='U')
  14.             return 0; //PUSH
  15.         else
  16.             return 1; //POP
  17.     }
  18.     else
  19.         return -1; // TOP
  20.  }
  21.  
  22.  pair<int,int> GetIndex(string func){
  23.     if (!Function(func)){
  24.         int i = 6;
  25.         int ans = (func[5]-'0');
  26.         while ( func[i] != ',' ){
  27.             ans *= 10;
  28.             ans += (func[i] - '0');
  29.             i++;
  30.         }
  31.         i+=2;
  32.         int post = (func[i-1] - '0');
  33.         while ( func[i] != ')' ){
  34.             post  *= 10;
  35.             post  += (func[i] - '0');
  36.             i++;
  37.         }
  38.         return make_pair(ans,post);
  39.     }
  40.     else {
  41.         int i = 5;
  42.         int ans = (func[4]-'0');
  43.         while ( func[i] != ')' ){
  44.             ans *= 10;
  45.             ans += (func[i] - '0');
  46.             i++;
  47.         }
  48.         return make_pair(ans,0);
  49.     }  
  50.  }
  51.  
  52.  inline void inter (string func){
  53.     if (!Function(func)){
  54.         vec[GetIndex(func).first-1].push(GetIndex(func).second);
  55.     }
  56.     else if(Function(func) > 0){
  57.         int index = GetIndex(func).first-1;
  58.         fout << vec[index].front() << " ";
  59.         vec[index].pop();
  60.     }
  61.     else {
  62.         fout << vec[GetIndex(func).first-1].front() << " ";
  63.     }
  64.  
  65.  }
  66.  
  67. int main(){    
  68.     ifstream fin ("input.txt");
  69.         int n, k;
  70.         fin >> n >> k;
  71.         vec = vector <queue <int> > (n);   
  72.    
  73.     string func;
  74.     getline(fin,func);
  75.     for (int i = 0 ; i < k; i++){
  76.         getline(fin,func);
  77.         inter (func);  
  78.     }  
  79.     fin.close();
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement