【数据结构】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。

 #include<bits/stdc++.h>
using namespace std; unordered_map <char,int> Hash;
stack <char> Ops;
stack <char> Nums;
bool f ; void Calc( ){
char v = Nums.top() ; Nums.pop();
char u = Nums.top() ; Nums.pop();
/*
if( !( Hash[u] == Hash[v] + 1 || Hash[v] == Hash[u] + 1 ) ){
f = false ; cout << u << " # " << v << endl;
cout << Hash[u] << " # " << Hash[v] << endl;
}
*/
if( Hash[u] > Hash[v] ) Nums.push(u) , Hash[u] -- ;
else Nums.push(v) , Hash[v] -- ;
Ops.pop();
} int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(NULL) , cout.tie(NULL); string s ;
cin >> s ; f = true;
char ch ;
int len = s.length() , v , m = ;
for(int i=;i<len;i++) m += ('a' <= s[i] && s[i] <= 'z' ); for(int i=;i<m;i++){
cin >> ch >> v ;
Hash[ch] = v ;
}
/*
for(int i=0;i<26;i++){
cout << 'a' + i << " " << Hash['a'+i] << endl;
}
*/ for(int i = ; i<len ; i++ ){
if( s[i] == '[' ){
Ops.push(s[i]);
}else if( 'a' <= s[i] && s[i] <= 'z' ){
Nums.push(s[i]);
}else if( s[i] == ']' ){
while( Ops.top() != '[' ) Calc() ;
Ops.pop();
}else if( s[i] == '-'){
Ops.push(s[i]);
}
/*else{
puts(" Invaild Operator \n");
}*/
} /*
for(int i=0;i<26;i++){
cout << char('a' + i) << " " << Hash['a'+i] << endl;
}
*/ //cout << s << endl;
if( f ){
//int t = 0 ;
for(int i=;i<;i++){
if( Hash['a'+i] != )
f = false ;
}
//if( t ) f = false ;
}
if( f ){
printf("Yes\n");
}else{
printf("No\n");
}
return ;
} /* [[[a-b]-c]-[d-e]]
a 3
b 0
c 0
d 1
e 0 */
上一篇:DOIS 2019 DevOps国际峰会北京站来袭~


下一篇:ArcGIS超级工具SPTOOLS-锐角检查,获得内角并判断是否凸多边形,获得线(面)两个折点方向