Search Here

Wednesday, September 9, 2015

UVa - 10194 - ( Football ( aka Soccer ) Solution ) [ With Solving Procedure ]

=>Try Yourself First. For help Scroll Down .

Ohh God . Thanks . After huge time of getting WA I got AC . Almost 10 times. But I don't care about it . I got AC . That's the Thing of Happiness. Now I am going to tell You how to Solve it?  Read the problem statement Very Very Carefully .

Clearance :
After reading the Problem . You got an Idea about which algorithm Should apply ?  . Yes. " Sorting " . But Sort with some Custom Condition . So what you have to do is - 


  • After taking the inputs into an array call STL " sort ( ) " and With a Comparing function Sort the Whole list as the Problem statement Said.
  • Take a Good care about Input and Output Handling System . This two suffer you a lot in solving this Problem . Read the " INPUT " Section of Problem Statement very carefully .This point suffers me a lot .
  • And keep your Head very Cool until you solve this Problem . After solving You can Dance . As I did . I solved it after 18 hours . A silly point take a time about 2-3 hours. So very careful about what you did and are doing .
Input Handing System
  • As the problem statement Stated, take a number TEST ( test cases )& N ( number of teams ).Use ' getchar() ' after taking N. Then, you have to take the Team Names . I would like to Suggest you to take this Team Names into a Map and Assign an Integer for each team. Then you have to take G( number of games played ). Again use a ' getchar()' here. Then take Game Result as a String . The run a loop from ' index = 0 ' to until finding '@' . In this loop before getting '#' take the Team Name and After getting '#' take the GN ( Number of Goals ) as integer . Then run another loop from ' index = index of '@' + 1 ' to the Length of String to take another Team's Goal Information . 
  • From the Goal's Information you will get the Winner , Looser, Tied Match, Number of Scored Goal of each Team, Number of Against Goal of each Team, Acquired Points of each Team, Number of Games each Team played .
  • As the Team Name any Character will Valid whose ASCII value id Greater than 31. That means from " Space " character of ASCII list to " 127 th Character " ( Except # and @ ) .
Algorithm Manipulation
  • As stated above a Custom Sort needed in this Problem . That's why you need a Structure Array which Contains the Parameter you need to Sort the Informations .  After creating a Structure Array Insert all the Parameters ( Name, Points, Number of Given Goals, Difference of Goals, Games Played, Number of Winning Match ) into that Array you need to Sort using " map iterator " . Then call STL " sort() " along with a " compare " function like below's :
  1.    sort(arr, arr+N, compare ); 
  • In that compare function use the condition according to You have to Sort...


Note For Lexicographical Order While Sorting :

  • As a Tie Breaker Team Names are Case Insensitive . That means , " BrAziL " and " bRaZil are the Same . You can use " strcasecmp() " function or You can use " strcmp() "after turning all letters into Uppercase OR Lowercase order .
Output Handling :
  • Output the Sorted List as you according to the Order as stated into the Output Specification . Print an Empty Line between Each test Cases .
!! Warning : Clear the Map every time . Set the memory elements of all Array ( Except Structure Array ) as 0.  Be careful about Spacing . !! Keep yourself Free from Tension . Don't Panic If You got WA . Go through on you code once and think again what you have missed.  !!


// The language of Code is Flexible to read for the Bangladeshis . I have used here " KHATI BANGLA SHOBDO " .
========================================================================================
Code :

#include<bits/stdc++.h>
#define CLR(a,b) memset(a, b, sizeof(a))
using namespace std;
map<string , int> teams;
map<string , int> :: iterator it;
int team_goal_dise[100], team_goal_khaise[100], total_khelse[100];
int winner[100], point[100], looser[100], draw[100];
struct data{
string teamname;
int point, goaldise, goal_diff, khelse, jitse;
};
bool cmp(data one, data two)
{
char bread[105], butter[105];
if(one.point==two.point)
{
if(one.jitse==two.jitse)
{
if(one.goal_diff==two.goal_diff)
{
if(one.goaldise == two.goaldise)
{
if(one.khelse==two.khelse)
{
int szbread = one.teamname.size();
int szbutter = two.teamname.size();
for(int i=0; i<szbread; i++)
bread[i] = tolower(one.teamname[i]);
for(int i=0; i<szbutter; i++)
butter[i] = tolower(two.teamname[i]);
return strcmp(bread, butter)<0;
}
else return one.khelse<two.khelse;
}
else return one.goaldise>two.goaldise;
}
else return one.goal_diff>two.goal_diff;
}
else return one.jitse>two.jitse;
}
else return one.point>two.point;
}
int main()
{
int test, n, m, goalone, goaltwo;
string name, score, adi1, adi2, tourn_name;
scanf("%d", &test);
getchar();
while(test--)
{
getline(cin,tourn_name);
//getchar();
teams.clear();
CLR(team_goal_dise, 0); CLR(team_goal_khaise, 0); CLR(looser,0);
CLR(total_khelse, 0); CLR(winner, 0); CLR(point, 0); CLR(draw, 0);
scanf("%d", &n);
getchar();
for(int i=0; i<n; i++)
{
getline(cin,name);
// cout<<name<<endl;
teams[name] = i;
}
scanf("%d",&m);
getchar();
for(int i=0; i<m; i++)
{
getline(cin,score);
//cout<<score<<endl;
int sz = score.size(), j;
goalone = 0; adi1 = "";
bool hashfound = false;
for(j=0; score[j]!='@'; j++)
{
if(score[j]=='#') { hashfound = true; continue; }
if(!hashfound)
{
adi1 = adi1 + score[j];
}
else if(isdigit(score[j]))
{
goalone = goalone*10 + (score[j]-'0');
}
}
adi2 ="";goaltwo = 0; hashfound = false;
for(int l=j+1; score[l]!='\0'; l++)
{
if(score[l]=='#') {hashfound = true; continue; }
if(hashfound)
{
adi2 = adi2 + score[l];
}
else if(isdigit(score[l]))
{
goaltwo = goaltwo*10 + (score[l]-'0');
}
}
//cout<<goalone<<' '<<adi1<<' '<<goaltwo<<' '<<adi2<<endl;
team_goal_dise[teams[adi1]] = team_goal_dise[teams[adi1]] + goalone;
team_goal_dise[teams[adi2]] = team_goal_dise[teams[adi2]] + goaltwo;
team_goal_khaise[teams[adi1]] = team_goal_khaise[teams[adi1]] + goaltwo;
team_goal_khaise[teams[adi2]] = team_goal_khaise[teams[adi2]] + goalone;
total_khelse[teams[adi1]]++;
total_khelse[teams[adi2]]++;
if(goalone>goaltwo)
{
winner[teams[adi1]]++;
looser[teams[adi2]]++;
point[teams[adi1]] = point[teams[adi1]] + 3;
}
else if(goalone<goaltwo)
{
winner[teams[adi2]]++;
looser[teams[adi1]]++;
point[teams[adi2]] = point[teams[adi2]] + 3;
}
else
{
draw[teams[adi1]]++;
draw[teams[adi2]]++;
point[teams[adi1]] = point[teams[adi1]] + 1;
point[teams[adi2]] = point[teams[adi2]] + 1;
}
}
/*for(it=teams.begin(); it!=teams.end(); ++it)
{
cout<<it->first<<' '<<point[teams[it->first]]<<' ';
cout<<total_khelse[teams[it->first]]<<' '<<winner[teams[it->first]]<<' ';
cout<<draw[teams[it->first]]<<' '<<looser[teams[it->first]]<<' ';
cout<<team_goal_dise[teams[it->first]]<<' '<<team_goal_khaise[teams[it->first]]<<endl;
}*/
data total_teams[100];
for(it=teams.begin(); it!=teams.end(); ++it)
{
total_teams[it->second].point = point[it->second];
total_teams[it->second].jitse = winner[it->second];
total_teams[it->second].goal_diff = team_goal_dise[it->second] - team_goal_khaise[it->second];
total_teams[it->second].goaldise = team_goal_dise[it->second];
total_teams[it->second].khelse = total_khelse[it->second];
total_teams[it->second].teamname = it->first;
}
sort(total_teams, total_teams+n, cmp);
cout<<tourn_name<<endl;
for(int i=0; i<n; i++)
{
cout<<i+1<<')'<<' '<<total_teams[i].teamname<<' '<<total_teams[i].point<<"p, "<<total_teams[i].khelse<<"g ("<<winner[teams[total_teams[i].teamname]]<<'-';
cout<<draw[teams[total_teams[i].teamname]]<<'-'<<looser[teams[total_teams[i].teamname]]<<"), "<<total_teams[i].goal_diff<<"gd (";
cout<<team_goal_dise[teams[total_teams[i].teamname]]<<'-'<<team_goal_khaise[teams[total_teams[i].teamname]]<<')'<<endl;
}
if(test)
printf("\n");
//CLR(total_teams,0);
}
return 0;
}



======================================================================================================

For Critical Inputs And OutputsCheck This Critical Inputs 

=>Question or Need Help ?? Leave a Comment .

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey ...
    { hashfound = true; continue; }
    if(!hashfound)
    {
    adi1 = adi1 +
    score[j];
    }

    - About this statement.

    See the Given Scoreline .. The Team Name & Number of Goals are Separated by only this Hash ( # )[ That means ' # ' is a Separator between Team Name & Goal Scored ] .... Let "adi1" & "adi2" is two Additional String to store two team's name . So, When if we found a Hash ' # ' then we can say that We get a Team's Name Already . And now we have to take that team's Scored_Goal.

    For example Barcelona#2@2#Real_Madrid .[ ' # ' is the separator that we have to use to take team's Name & Goal Information ]. Hope Got it .....
    For more leave a Comment .

    ReplyDelete