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;
}

Tournament Chart【模拟+vector+map+string】的更多相关文章

  1. programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation

    编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...

  2. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  3. 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest A Email Aliases(模拟STL vector+map)

    Email AliasesCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:524288KB     ...

  4. POJ 3087 Shuffle'm Up【模拟/map/string】

    Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14471 Accepted: 6633 Descrip ...

  5. PAT 1039 Course List for Student (25分) 使用map<string, vector<int>>

    题目 Zhejiang University has 40000 students and provides 2500 courses. Now given the student name list ...

  6. c++ list, vector, map, set 区别与用法比较

    http://blog.csdn.net/alex_xhl/article/details/37692297 List封装了链表,Vector封装了数组, list和vector得最主要的区别在于ve ...

  7. UVA 156:Ananagrams (vector+map+sort)

    题意:一大堆单词中间有空格隔开,以'#'结束输出,问只出现一次的的单词有哪些(如果两个具有相同的长度,相同的字母也算是相同的,不区分大小写,如:noel和lone属于一个单词出现两次).最后按照字典序 ...

  8. list, vector, map, set 区别与用法比较

    List封装了链表,Vector封装了数组, list和vector得最主要的区别在于vector使用连续内存存储的,他支持[]运算符,而list是以链表形式实现的,不支持[]. Vector对于随机 ...

  9. 【数据结构】Tournament Chart

    Tournament Chart 题目描述 In 21XX, an annual programming contest, Japan Algorithmist GrandPrix (JAG) has ...

随机推荐

  1. 用了这么多年的 Java 泛型,你对它到底有多了解?

    作为一个 Java 程序员,日常编程早就离不开泛型.泛型自从 JDK1.5 引进之后,真的非常提高生产力.一个简单的泛型 T,寥寥几行代码, 就可以让我们在使用过程中动态替换成任何想要的类型,再也不用 ...

  2. HDU4315 Climbing the Hill

    题目链接:https://cn.vjudge.net/problem/HDU-4315 知识点: 博弈论 题目大意: \(Alice\) 和 \(Bob\) 轮流指挥 \(N\) 个人爬山,这 \(N ...

  3. Oracle 中序列使用

    转 https://www.cnblogs.com/21-forever/p/11265924.html 序列: 1.Oracle是不支持自增长的: ①.序列是用于生成唯一.连续序号的对象: ②.序列 ...

  4. SEPC:使用3D卷积从FPN中提取尺度不变特征,涨点神器 | CVPR 2020

    论文提出PConv为对特征金字塔进行3D卷积,配合特定的iBN进行正则化,能够有效地融合尺度间的内在关系,另外,论文提出SEPC,使用可变形卷积来适应实际特征间对应的不规律性,保持尺度均衡.PConv ...

  5. ## 0521Day04内部类

    [重点] Math公式 静态导入 正则表达式 内部类 访问修饰符 [Math] Math包的相关方法: round:四舍五入:-10.9==>-11/-11.2==>-11 floor:向 ...

  6. 【JUC】阻塞队列&生产者和消费者

    阻塞队列 线程1往阻塞队列添加元素[生产者] 线程2从阻塞队列取出元素[消费者] 当队列空时,获取元素的操作会被阻塞 当队列满时,添加元素的操作会被阻塞 阻塞队列的优势:在多线程领域,发生阻塞时,线程 ...

  7. [安卓基础] 004.运行app

    运行你的app 这篇课程会教你: 1.如何在设备上运行你的app. 2.如何在模拟器上运行你的app. 当然,在学习之前,你还需要知道: 1.如何使用设备. 2.如何使用模拟器. 3.管理你的项目. ...

  8. 删除节点与插入节点 & innerHTML

    1.测试removeChild()方法: 删除节点dom9.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" &q ...

  9. Misdirection: 1靶机writeup

    看下端口 nmap -A 172.16.61.131 一些坑3306无法访问,80,web2py漏洞无法利用 利用dirb遍历网站路径 得到下面命令执行漏洞 http://172.16.61.131: ...

  10. 【算法】单元最短路径之Bellman-Ford算法和SPFA算法

    SPFA是经过对列优化的bellman-Ford算法,因此,在学习SPFA算法之前,先学习下bellman-Ford算法. bellman-Ford算法是一种通过松弛操作计算最短路的算法. 适用条件 ...