zoukankan      html  css  js  c++  java
  • 【数据结构】Tournament Chart

    Tournament Chart

    题目描述

    In 21XX, an annual programming contest, Japan Algorithmist GrandPrix (JAG) has become one of the most popular mind sports events.

    JAG is conducted as a knockout tournament. This year, N contestants will compete in JAG. A tournament chart is represented as a string. '[[a-b]-[c-d]]' is an easy example. In this case, there are 4 contestants named a, b, c, and d, and all matches are described as follows:

    Match 1 is the match between a and b.
    Match 2 is the match between c and d.
    Match 3 is the match between [the winner of match 1] and [the winner of match 2].
    More precisely, the tournament chart satisfies the following BNF:

    <winner> ::= <person> | "[" <winner> "-" <winner> "]"
    <person> ::= "a" | "b" | "c" | ... | "z"
    You, the chairperson of JAG, are planning to announce the results of this year's JAG competition. However, you made a mistake and lost the results of all the matches. Fortunately, you found the tournament chart that was printed before all of the matches of the tournament. Of course, it does not contains results at all. Therefore, you asked every contestant for the number of wins in the tournament, and got N pieces of information in the form of "The contestant ai won vi times".

    Now, your job is to determine whether all of these replies can be true.

    输入

    The input consists of a single test case in the format below.

    S
    a1 v1
    :
    aN vN
    S represents the tournament chart. S satisfies the above BNF. The following N lines represent the information of the number of wins. The (i+1)-th line consists of a lowercase letter ai and a non-negative integer vi (vi≤26) separated by a space, and this means that the contestant ai won vi times. Note that N (2≤N≤26) means that the number of contestants and it can be identified by string S. You can assume that each letter ai is distinct. It is guaranteed that S contains each ai exactly once and doesn't contain any other lowercase letters.

    输出

    Print 'Yes' in one line if replies are all valid for the tournament chart. Otherwise, print 'No' in one line.

    样例输入

    [[m-y]-[a-o]]
    o 0
    a 1
    y 2
    m 0
    

    样例输出

    Yes

     

    【题解】

    类似于表达式求值,每次都把比赛中较大者减1,最后判断是否全为0即为Yes。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3  
     4 unordered_map <char,int> Hash;
     5 stack <char> Ops;
     6 stack <char> Nums;
     7 bool f ;
     8  
     9 void Calc( ){
    10     char v = Nums.top() ; Nums.pop();
    11     char u = Nums.top() ; Nums.pop();
    12     /*
    13     if( !( Hash[u] == Hash[v] + 1  || Hash[v] == Hash[u] + 1 ) ){
    14         f = false ;
    15  
    16         cout << u << " # " << v << endl;
    17         cout << Hash[u] << " # " << Hash[v] << endl;
    18     }
    19     */
    20     if( Hash[u] > Hash[v] ) Nums.push(u) , Hash[u] -- ;
    21     else Nums.push(v) , Hash[v] -- ;
    22     Ops.pop();
    23 }
    24  
    25 int main()
    26 {
    27     ios_base :: sync_with_stdio(false);
    28     cin.tie(NULL) , cout.tie(NULL);
    29  
    30     string s ;
    31     cin >> s ;
    32  
    33     f = true;
    34     char ch ;
    35     int len = s.length() , v , m = 0 ;
    36     for(int i=0;i<len;i++)  m += ('a' <= s[i] && s[i] <= 'z' );
    37  
    38  
    39  
    40     for(int i=0;i<m;i++){
    41         cin >> ch >> v ;
    42         Hash[ch] = v ;
    43     }
    44     /*
    45     for(int i=0;i<26;i++){
    46         cout << 'a' + i << " " << Hash['a'+i] << endl;
    47     }
    48     */
    49  
    50     for(int  i = 0 ; i<len ; i++ ){
    51         if( s[i] == '[' ){
    52             Ops.push(s[i]);
    53         }else if( 'a' <= s[i] && s[i] <= 'z' ){
    54             Nums.push(s[i]);
    55         }else if( s[i] == ']' ){
    56             while( Ops.top() != '[' ) Calc() ;
    57             Ops.pop();
    58         }else if( s[i] == '-'){
    59             Ops.push(s[i]);
    60         }
    61         /*else{
    62             puts(" Invaild Operator 
    ");
    63         }*/
    64     }
    65  
    66     /*
    67     for(int i=0;i<26;i++){
    68         cout << char('a' + i) << " " << Hash['a'+i] << endl;
    69     }
    70     */
    71  
    72     //cout << s << endl;
    73     if( f ){
    74         //int t = 0 ;
    75         for(int i=0;i<26;i++){
    76             if( Hash['a'+i] != 0 )
    77                 f = false ;
    78         }
    79         //if( t ) f = false ;
    80     }
    81     if( f ){
    82         printf("Yes
    ");
    83     }else{
    84         printf("No
    ");
    85     }
    86     return 0;
    87 }
    88  
    89  
    90 /*
    91  
    92 [[[a-b]-c]-[d-e]]
    93 a 3
    94 b 0
    95 c 0
    96 d 1
    97 e 0
    98  
    99 */
    View Code
  • 相关阅读:
    mp3播放器(四)(从服务器端下载mp3文件+service的应用+SDcard操作)
    mp3播放器(二)(adapter模式将显示在屏幕上)
    HTML日记一(表格,图片,超链接,无序列表,frameset框架集)
    HTML日记三(地图图片映射)
    聚集索引和非聚集索引[转]
    linq 解决winForm中控件CheckedListBox操作的问题。(转载)
    C#抓取网页数据、分析并且去除HTML标签(转载)
    Cachecontrol使用:header('Cachecontrol:private')
    缓存DataSet(转载)
    jquery调用基于.NET Framework 3.5的WebService返回JSON数据 (转)
  • 原文地址:https://www.cnblogs.com/Osea/p/11397649.html
Copyright © 2011-2022 走看看