Tournament Chart【模擬+vector+map+string】

Tournament Chart

傳送門:鏈接  來源:UPC10889

題目描述

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

題目大意:

給出上面的BNF公式(看不懂),猜測應該是一個公式類似: [ [a-b]-c ] (這個比樣例有普遍性)

每個 - 代表前面的隊伍要和後面的隊伍比賽,獲勝的再與後面的比,在上面的例子中,是a先與b比賽,獲勝者再與c比。

題目給出每個隊伍最終的獲勝次數,讓你判斷這個數據有沒有可能是正確的。

解題思路:

按照下圖一步步模擬就行了,因爲要用到刪除操作,直接用string不太好實現,可以用 vector<char>,這篇博很詳細:傳送門 。

具體實現就是每次遍歷vector,找到兩邊都是字母的減號,刪除【vc[i-2],vc[i+2]】的內容,再把勝場多的字符插入進去(也可以先修改字符再刪內容,只是區間要變化一下),並將他對應的勝場-1(map數組在這很好用)。

判斷條件:

兩個比賽隊伍,肯定會有一個會輸,也就是勝場爲0,否則就輸出NO。

如果兩個比賽的隊伍勝場相同,也要輸出NO。

最後只剩下的一個隊伍對應的勝場應該也是0,否則輸出NO。

AC代碼:

感謝隊友給找的bug:https://me.csdn.net/qq_43559193

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    cin>>a; ///輸入字符串
    int la=a.size(),cnt=0;
    for(int i=0;i<la;i++){
        if(a[i]>='a'&&a[i]<='z'){
            cnt++;
        }
    }
    vector<char>vc; 
    for(int i=0;i<la;i++){ ///字符串內容賦值到vector中(輸入的時候直接往裏面存也可以)
        vc.push_back(a[i]);
    }
    map<char,int>mp;
    for(int i=0;i<cnt;i++){ ///輸入勝場,存入mp數組
        int num;
        char id;
        cin>>id>>num;
        mp[id]=num;
    }
    while(vc.size()!=1){
        for(int i=0;i<vc.size();i++){
            if(vc[i]=='-'&&vc[i-1]>='a'&&vc[i-1]<='z'&&vc[i+1]>='a'&&vc[i+1]<='z'){
                if(!(mp[vc[i+1]]==0||mp[vc[i-1]]==0)||(mp[vc[i+1]]==mp[vc[i-1]])){
                    cout<<"No"<<endl; ///一定要判斷,不然會死循環
                    return 0;
                }
                if(mp[vc[i+1]]>mp[vc[i-1]]){ ///減號右邊的勝場多
                    vc[i+2]=vc[i+1];///先修改值
                    mp[vc[i+1]]--;
                    vc.erase(vc.begin()+i-2,vc.begin()+i+2); ///刪除區間內容
                    break;
                }else if(mp[vc[i+1]]<mp[vc[i-1]]){///減號左邊的勝場多
                    vc[i+2]=vc[i-1];
                    mp[vc[i-1]]--;
                    vc.erase(vc.begin()+i-2,vc.begin()+i+2);
                    break;
                }
            }
        }
    }
    if(mp[vc[0]]==0) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章