zoukankan      html  css  js  c++  java
  • UVa 11995 I Can Guess the Data Structure!

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

    直接用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 }
    代码君
  • 相关阅读:
    Vue $emit()不触发方法的原因
    java 定时任务之一 @Scheduled注解(第一种方法)
    Dubbo的使用及原理浅析.
    Android App 安全的HTTPS 通信
    详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)
    IDEA 2018集成MyBatis Generator 插件 详解
    自建证书配置HTTPS服务器
    Jsoup(一)Jsoup详解(官方)
    Android使用最小宽度限定符时最小宽度的计算
    可显示行号的log工具
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4339911.html
Copyright © 2011-2022 走看看