uva 10194 Football (aka Soccer)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid;=8&page;=show_problem&problem;=1135 题意:给出球队的名字和比赛的信息,得出stanging 思路:字符串处理。需要注意的是多组数据记得初始化多次,以及比较字典序的时候team name是大小写补敏感的。

/* ***********************************************
Author :111qqz
Created Time :2016年01月26日 星期二 15时47分36秒
File Name :code/uva/10194.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E3+5;
1string game[N];
2map<string,int>TeamToId;
3struct Team
4{
5    string nam;
6    string lowname;
7    int a,b,c,d,e,f,g,h,i;
 1    bool operator<(Team p)const
 2    {
 3	if (b>p.b) return true;
 4	if (b==p.b&&d>p.d) return true;
 5	if (b==p.b&&d==p.d&&g>p.g) return true;
 6	if (b==p.b&&d==p.d&&g==p.g&&h>p.h) return true;
 7	if (b==p.b&&d==p.d&&g==p.g&&h==p.h&&c<p.c) return true;
 8	if (b==p.b&&d==p.d&&g==p.g&&h==p.h&&c==p.c&&lowname<p.lowname) return true; //把第一个p.b写成p.d。。
 9	return false;
10    }
1}team[35];
2int g,n;
3string touname;
 1void init()
 2{
 3    for ( int i = 0 ; i<=30 ; i++)
 4    {
 5	team[i].a=0;
 6	team[i].b=0;
 7	team[i].c=0;
 8	team[i].d=0;
 9	team[i].e=0;
10	team[i].f=0;
11	team[i].g=0;
12	team[i].h=0;
13	team[i].i=0;
14    }
15}
 1string tolow( string x)
 2{
 3    int len = x.length();
 4    for ( int i = 0 ; i < len ; i++) x[i]=tolower(x[i]);
 5    return x;
 6}
 7int main()
 8{
 9	#ifndef  ONLINE_JUDGE 
10	freopen("code/in.txt","r",stdin);
11  #endif
 1	int T;
 2	cin>>T;
 3	getchar();
 4	while (T--)
 5	{
 6	    getline(cin,touname);
 7	    cout<<touname<<endl;
 8	    TeamToId.clear();
 9	    scanf("%d",&n);
10	    getchar();
11	    init();
12	    for ( int i = 0 ; i < n ; i++)
13	    {
14		getline(cin,team[i].nam);
15		team[i].lowname=tolow(team[i].nam); //一开始没看到大小写比较字典序的时候不敏感这条...
16		TeamToId[team[i].nam]=i;
17	    }
 1	    //check teamname input
 2	  //  for ( int i = 0 ; i < n ; i++) cout<<i<<" "<<team[i].lowname<<endl;
 3	    //check map of teamtoid
 4	  //  for ( int i = 0 ; i < n ; i++) cout<<i<<""<<TeamToId[team[i].nam]<<endl;
 5	    scanf("%d",&g);
 6	    getchar();
 7	    for (  int i = 0 ; i < g ; i++)
 8	    {
 9		getline(cin,game[i]);
10	    }
11	    //check game input
12	   // for ( int i = 0 ; i < g ; i++) cout<<i<<" "<<game[i]<<endl;
13	    for ( int i = 0 ; i < g ; i++)
14	    {
 1		int p1 = game[i].find('#');
 2		int p2 = game[i].find('@');
 3		int p3 = game[i].find('#',p2+1);
 4		string sformers = game[i].substr(p1+1,p2-p1-1);
 5		string slatters = game[i] .substr(p2+1,p3-p2-1);
 6		int formers,latters;
 7		sscanf(sformers.c_str(),"%d",&formers);
 8		sscanf(slatters.c_str(),"%d",&latters);
 9		string formerteam = game[i].substr(0,p1);
10		string latterteam = game[i].substr(p3+1);
11		int formerid = TeamToId[formerteam];
12		int latterid = TeamToId[latterteam];
1	//	cout<<"i:"<<i<<" "<<formerteam<<" "<<latterteam<<endl;
2		if (formers==latters)
3		{
4		    team[formerid].e++;
5		    team[latterid].e++;  //平局次数++
 1		    team[formerid].b++;
 2		    team[latterid].b++; //平局各得1分
 3		}
 4		 if(formers>latters)
 5		{
 6		    team[formerid].d++;
 7		    team[latterid].f++;
 8		    team[formerid].b+=3;
 9		}
10		if (formers<latters)
11		{
12		    team[formerid].f++;
13		    team[latterid].d++;
14		    team[latterid].b+=3;
15		}
16		team[formerid].h+=formers;
17		team[latterid].h+=latters;
18		team[formerid].c++;
19		team[latterid].c++;
20		team[formerid].i+=latters;
21		team[latterid].i+=formers;
1		//check each game
2	//	cout<<"formteam:"<<formerid<<" "<<formerteam<<endl;
3	//	cout<<"latterteam:"<<latterid<<" "<<latterteam<<endl; ok.
4	//	cout<<"formers:"<<formers<<endl;
5	//	cout<<"lattets:"<<latters<<endl;                      ok.
6	    }
7	    for ( int i = 0 ; i < n ; i++) team[i].g=team[i].h-team[i].i;
	    sort(team,team+n);
	    for ( int i = 0 ; i < n ; i++) team[i].a=i+1;
1	    for ( int i = 0 ; i < n ; i++)
2	    {
3		printf("%d) ",team[i].a);
4		cout<<team[i].nam<<" ";
5		printf("%dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",team[i].b,team[i].c,team[i].d,team[i].e,team[i].f,team[i].g,team[i].h,team[i].i);
6	    }
1	    if (T)
2	    {
3		puts("");
4	    }
5	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}