Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #define Integer long long int
- #define TRACE(x) cout << #x << " : " << x << endl
- using namespace std;
- const string digit = "0123456789";
- const string letter = "abcdefg_hijklmnop_qrs_tuv_wx_yz_ABCDEFG_HIJKLMNOP_QRS_TUV_WX_YZ";
- const char zero = '0';
- const int matchNumber = 12;
- const int teamNumber = 4;
- vector<string> name;
- Integer score[teamNumber];
- Integer goals[teamNumber];
- Integer goalDiff[teamNumber];
- Integer awaygoal[teamNumber];
- void clear() {
- name.clear();
- for (int i = 0; i < teamNumber; i++) {
- score[i] = 0;
- goals[i] = 0;
- goalDiff[i] = 0;
- awaygoal[i] = 0;
- }
- }
- int first(const string &str, int s_index, const string &match) {
- for (int i = s_index; i < str.size(); i++) {
- char c = str[i];
- for (int j = 0; j < match.size(); j++)
- if (c == match[j])
- return i;
- }
- return -1;
- }
- bool isDigit(char c) {
- for (int i = 0; i < digit.size(); i++)
- if (c == digit[i])
- return true;
- return false;
- }
- pair<string, string> extractTeams(const string &line) {
- pair<string, string> res;
- int fd = first(line, 0, digit) - 2;
- res.first = line.substr(0, fd + 1);
- int dot = first(line, 0, ".");
- int beg = first(line, dot, letter);
- if (!isDigit(line[beg - 2]))
- beg--;
- res.second = line.substr(beg);
- return res;
- }
- pair<Integer, Integer> extractGoals(const string &line) {
- pair<Integer, Integer> res;
- Integer n = 0;
- int ind = first(line, 0, digit);
- while (isDigit(line[ind])) {
- int val = int(line[ind]) - int(zero);
- n *= 10;
- n += val;
- ind++;
- }
- res.first = n;
- n = 0;
- int dot = first(line, 0, ".");
- ind = first(line, dot, digit);
- while (isDigit(line[ind])) {
- int val = int(line[ind]) - int(zero);
- n *= 10;
- n += val;
- ind++;
- }
- res.second = n;
- return res;
- }
- int numberOfTeam(string team) {
- for (int i = 0; i < name.size(); i++)
- if (team == name[i])
- return i;
- return -1;
- }
- void update(int a, int b, Integer c, Integer d) {
- goals[a] += c;
- goals[b] += d;
- goalDiff[a] += c - d;
- goalDiff[b] += d - c;
- awaygoal[b] += d;
- if (c == d) {
- score[a]++;
- score[b]++;
- } else if (c > d)
- score[a] += 3;
- else
- score[b] += 3;
- if (a == 0 || b == 0) {
- // TRACE(a);
- // TRACE(b);
- // TRACE(c);
- // TRACE(d);
- // TRACE(score[0]);
- // cout<<"---------------\n";
- }
- }
- void read() {
- for (int i = 0; i < matchNumber; i++) {
- string line;
- getline(cin, line);
- pair<string, string> teams = extractTeams(line);
- string homeTeam = teams.first;
- string awayTeam = teams.second;
- int a = numberOfTeam(homeTeam);
- if (a == -1) {
- name.push_back(homeTeam);
- a = name.size() - 1;
- }
- int b = numberOfTeam(awayTeam);
- if (b == -1) {
- name.push_back(awayTeam);
- b = name.size() - 1;
- }
- pair<Integer, Integer> goals = extractGoals(line);
- Integer c = goals.first;
- Integer d = goals.second;
- update(a, b, c, d);
- }
- }
- bool outRank(int a, int b) {
- if (score[a] == score[b]) {
- if (goalDiff[a] == goalDiff[b]) {
- if (goals[a] == goals[b])
- return (awaygoal[a] > awaygoal[b]);
- else
- return (goals[a] > goals[b]);
- } else
- return (goalDiff[a] > goalDiff[b]);
- } else
- return (score[a] > score[b]);
- }
- int best() {
- int res = 0;
- for (int i = 1; i < teamNumber; i++)
- if (outRank(i, res))
- res = i;
- return res;
- }
- int best(int firstTeam) {
- int res = 0;
- if (firstTeam == 0)
- res = 1;
- for (int i = 1; i < teamNumber; i++) {
- if (i == firstTeam)
- continue;
- if (outRank(i, res))
- res = i;
- }
- return res;
- }
- int main() {
- int t;
- cin>>t;
- string x;
- getline(cin,x);
- for (int i = 0; i < t; i++) {
- clear();
- read();
- int firstTeam = best();
- int secondTeam = best(firstTeam);
- // TRACE(firstTeam);
- // TRACE(name[firstTeam]);
- // TRACE(score[firstTeam]);
- // TRACE(goalDiff[firstTeam]);
- // TRACE(awaygoal[firstTeam]);
- // TRACE(secondTeam);
- // TRACE(name[secondTeam]);
- // TRACE(score[secondTeam]);
- // TRACE(goalDiff[secondTeam]);
- // TRACE(awaygoal[secondTeam]);
- // cout << outRank(firstTeam, secondTeam) << endl;
- cout << name[firstTeam] <<" ";
- cout << name[secondTeam] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement