这是我第一次在csdn上写博,由于今后读研的需要,以及我的研究生导师给我布置了大量Codeforces练习任务,我不得不重拾学得很烂的C++,本着记录研前学习的苦逼历程与收获,以及发扬极客的分享精神,我决定将学习的东西开始在博客上做记录总结,以后可能还会记录一些关于OpenCV在iOS上的应用,本人非大神,重在交流学习。
Codeforces Round #281 (Div. 2) A - Vasya and Football,题目如下:
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the firstmoment of time when he would receive a red card from Vasya.
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
- first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
- then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
- then goes the player's number m (1 ≤ m ≤ 99);
- then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
For each event when a player received his first red card in a chronological order print a string containing the following information:
- The name of the team to which the player belongs;
- the player's number in his team;
- the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
MC CSKA 9 28 a 3 y 62 h 25 y 66 h 42 y 70 h 25 y 77 a 4 y 79 a 25 y 82 h 42 r 89 h 16 y 90 a 13 r
MC 25 70 MC 42 82 CSKA 13 90
输入格式:主队名称
客队名称
本场总犯规记录数
犯规时间 主/客队 球员号 黄/红牌
。。。
输出格式:主/客队 球员号 犯规时间
。。。
当清楚了输入输出的内容之后,我们大概知道这道题目的关键就是查找关键信息,最后再以另一种形式输出,为了方便信息的输入,我建立了一个records的结构体来存储犯规信息,同时也为输出格式建立了结构体outputline:
struct records {
int time;
char team;
int playerNumber;
char colorOfcard;
};
struct outputline {
string team;
int playerNumber;
int time;
};
为了减少循环遍历的次数,我选择输入一条记录便对记录进行判断,并且将判断后可能的输出存入vector数组中,于是,这里就出现了一个问题,我起初像书上那样将结构体声明在主函数内部,并且声明了一个outputline类型(输出的格式)的vector,在自己电脑上编译时没有问题,但是提交到codeforces上就出现了编译错误,错误如下:
Can't compile file: program.cpp: In function 'int main()': program.cpp:25:27: error: template argument for 'template<class _Alloc> class std::allocator' uses local type 'main()::outputline' program.cpp:25:27: error: trying to instantiate 'template<class _Alloc> class std::allocator' program.cpp:25:27: error: template argument 2 is invalid program.cpp:25:41: error: invalid type in declaration before ';' token program.cpp:37:26: error: request for member 'push_back' in 'outputsline1', which is of non-class type 'int' program.cpp:51:30: error: request for member 'push_back' in 'outputsline1', which is of non-class type 'int' program.cpp:55:38: error: request for member 'size' in 'outputsline1', which is of non-class type 'int' program.cpp:56:31: error: invalid types 'int[int]' for array subscript program.cpp:57:31: error: invalid types 'int[int]' for array subscript program.cpp:58:31: error: invalid types 'int[int]' for array subscript
查了一下午才发现是因为自己将结构体声明在了main函数里,而按C98标准来说,局部类不是外部链接属性的,不可以成为模板的参数。所以将结构体声明在mian函数之外即可,同样,deque的参数也不可以为局部类。完整程序如下:
#include
#include
#include
#include
#include
using namespace std; struct records { int time; char team; int playerNumber; char colorOfcard; }; struct outputline { string team; int playerNumber; int time; }; int main(){ //using namespace std; string teamHome; string teamAway; int fouls; cin >> teamHome >> teamAway >> fouls; records r[fouls]; std::vector
outputsline1; for (int i = 0; i < fouls; i ++) { cin >> r[i].time; cin >> r[i].team; cin >> r[i].playerNumber; cin >> r[i].colorOfcard; outputline temp; if (r[i].colorOfcard == 'r') { int flag = 0; for (int j = 0; j < outputsline1.size(); j ++) { if (outputsline1[j].playerNumber == r[i].playerNumber && outputsline1[j].team == (r[i].team == 'h' ? teamHome : teamAway) ) { flag = 1; break; } } if (flag == 0) { temp.team = (r[i].team == 'h' ? teamHome : teamAway); temp.playerNumber = r[i].playerNumber; temp.time = r[i].time; outputsline1.push_back(temp); } } else { int flag = 0; for (int j = 0; j < outputsline1.size(); j ++) { if (outputsline1[j].playerNumber == r[i].playerNumber && outputsline1[j].team == (r[i].team == 'h' ? teamHome : teamAway) ) { flag = 1; break; } } if (flag == 0) { int ans = 1; for (int j = 0; j < i; j ++) { if (r[j].playerNumber == r[i].playerNumber && r[j].team == r[i].team && r[j].colorOfcard == 'y') { ans ++; } } if (ans == 2) { temp.team = (r[i].team == 'h' ? teamHome : teamAway); temp.playerNumber = r[i].playerNumber; temp.time = r[i].time; outputsline1.push_back(temp); } } } } for (int i = 0; i < outputsline1.size(); i ++) { cout << outputsline1[i].team << " "; cout << outputsline1[i].playerNumber << " "; cout << outputsline1[i].time << "\n"; } }