uva - 10194 - Football (aka Soccer) Solution

uva - 10194 - Football (aka Soccer) Solution

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<string.h>
#include<stdio.h>

using namespace std;

class Team{
public:
string name;
int b, c, d, e, f, g, h, i;
Team(){
b = c = d = e = f = g = h = i = 0;
}
};

void split_string(string &str, string delimiter, vector<string> &result){
string temp,temp2;
temp.resize(str.length());
copy(str.begin(), str.end(),temp.begin());
int pos;
while (true){
if (temp.length() == 0)
break;
pos = temp.find(delimiter);
if (pos == -1){
result.push_back(temp);
break;
}
temp2 = temp.substr(0, pos);
if (temp2.length()!=0)
result.push_back(temp2);
temp = temp.substr(pos + delimiter.length());
}
}

int string_to_int(string str){
int i, sum = 0, len = str.length(),mul=1;
for (i = len - 1; i >= 0; i--){
sum += (str[i] - 48)*mul;
mul = mul * 10;
}
return sum;
}

void update_team_result(Team *t,int goal1,int goal2){
if (goal1 > goal2){
t->b += 3;
t->d += 1;
}
else if (goal1 == goal2){
t->b += 1;
t->e++;
}
else{
t->f++;
}
t->c++;
t->h = t->h + goal1;
t->i = t->i + goal2;
t->g = t->g + (goal1 - goal2);
}

void convertTolower(string &str){
for (int i = 0; i < str.length(); i++){
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] += 32;
}
}

bool team_sort(Team *team1, Team *team2){
if (team1->b > team2->b)
return true;
if (team1->b < team2->b)
return false;

if (team1->d > team2->d)
return true;
if (team1->d < team2->d)
return false;

if (team1->g > team2->g)
return true;
if (team1->g < team2->g)
return false;

if (team1->h > team2->h)
return true;
if (team1->h < team2->h)
return false;

if (team1->c < team2->c)
return true;
if (team1->c > team2->c)
return false;
string name1 = team1->name;
string name2 = team2->name;
convertTolower(name1);
convertTolower(name2);
if (name1< name2)
return true;
return false;
}

int main() {
freopen("input.txt", "r", stdin);
int kase, i, j, n, m,goal1,goal2;
string title,str,team1,team2,score;
map<string, Team*> mymap;
map<string, Team*>::iterator mit;
vector<string> vec;
vector<Team*> vec_team;
cin >> kase;
getchar();
while (kase--){
getline(cin, title);
cin >> n;
getchar();
mymap.clear();
for (i = 0; i < n; i++){
getline(cin, str);
Team *t = new Team;
t->name = str;
mymap[str] = t;
}
cin >> m;
getchar();
for (i = 0; i < m; i++){
getline(cin, str);
vec.clear();
split_string(str, "#", vec);
team1 = vec[0];
score = vec[1];
team2 = vec[2];
vec.clear();
split_string(score, "@", vec);
goal1 = string_to_int(vec[0]);
goal2 = string_to_int(vec[1]);
update_team_result(mymap[team1], goal1, goal2);
update_team_result(mymap[team2], goal2, goal1);
}
vec_team.clear();
for (mit = mymap.begin(); mit != mymap.end(); mit++)
vec_team.push_back(mit->second);
for (i = 0; i < n; i++){
for (j = i+1; j < n; j++){
if (!team_sort(vec_team[i], vec_team[j])){
Team *t = vec_team[i];
vec_team[i] = vec_team[j];
vec_team[j] = t;
}
}
}
cout << title << endl;
Team *t;
for (i = 0; i < n; i++){
t = vec_team[i];
cout << i + 1 << ") " << t->name << " " << t->b << "p, " << t->c << "g (" << t->d << "-" << t->e << "-" << t->f << "), " << t->g << "gd (" << t->h << "-" << t->i << ")" << endl;
}

for (i = 0; i < n; i++)
delete vec_team[i];

if (kase > 0)
cout << endl;
}
return 0;
}

Comments

Popular posts from this blog

uva 679 - Dropping Balls Solution

uva 481 - What Goes Up Solution

uva-10077 Solution --- The Stern-Brocot Number System