這是個簡單的字符串處理題目。看題目,數據應該不是很大,直接暴力處理可以過。如果為了加快搜索速度,在中間輸入過程中排序,
再二分很麻煩,速度也快不了多少,因為只是輸入的過程中需要查找。但是,這個題其實很好用map做,代碼量可以少很多,也很簡潔。
寫這篇blog的目的是為了提醒自己,容易題再這樣錯下去,真的很傷人心,學什么都沒必要了,當時打算繼續搞ACM的目的之一就是
為了提高代碼正確率。這個題,不僅細節部分沒看清楚,而且寫代碼時候把比較函數里面的one.nLost寫成了one.nGet,查錯了1個多
小時,還讓隊友幫忙查錯了好久,真的很無語。寫程序確實可以debug,但是這也讓我養成了很嚴重的依賴debug的習慣。
人生不可以debug,人生不可以重來。記得以前很多次很多事情就是開始無所謂,后面悲催到底,無限后悔。
代碼如下:
1 #include <stdio.h> 2 #include <string.h>
3 #include <string>
4 #include <map>
5 #include <vector>
6 #include <algorithm>
7 #define MAX (100)
8 using std::map;
9 using std::string;
10 using std::vector;
11 using std::sort;
12
13 struct INFO
14 {
15 INFO()
16 {
17 nScore = nGet = nLost = 0;
18 }
19
20 string strName;
21 int nScore;
22 int nGet;
23 int nLost;
24 bool operator < (const INFO& one) const
25 {
26 if (nScore != one.nScore)
27 {
28 return nScore > one.nScore;
29 }
30 else if (nGet - nLost != one.nGet - one.nLost)//這里把one.nLost寫成了one.nGet
31 {
32 return nGet - nLost > one.nGet - one.nLost;
33 }
34 else if (nGet != one.nGet)
35 {
36 return nGet > one.nGet;
37 }
38 else
39 {
40 return strName < one.strName;
41 }
42 }
43 };
44
45 int main()
46 {
47 int nN;
48
49 //freopen("in.txt", "r", stdin);
50 //freopen("out.txt", "w", stdout);
51 while (scanf("%d", &nN) == 1)
52 {
53 int nLast = nN * (nN - 1);
54 char szOne[MAX];
55 char szTwo[MAX];
56 int nOne, nTwo;
57
58 map<string, INFO> myMap;
59 for (int i = 0; i < nLast; ++i)
60 {
61 scanf("%s %*s %s %d:%d", szOne, szTwo, &nOne, &nTwo);
62 //printf("%s %s %d %d\n", szOne, szTwo, nOne, nTwo);
63
64 string strOne = szOne;
65 myMap[strOne].strName = strOne;
66 myMap[strOne].nGet += nOne;
67 myMap[strOne].nLost += nTwo;
68
69 string strTwo = szTwo;
70 myMap[strTwo].strName = strTwo;
71 myMap[strTwo].nGet += nTwo;
72 myMap[strTwo].nLost += nOne;
73
74 if (nOne > nTwo)
75 {
76 myMap[strOne].nScore += 3;
77 }
78 else if (nOne == nTwo)
79 {
80 myMap[strOne].nScore += 1;
81 myMap[strTwo].nScore += 1;
82 }
83 else
84 {
85 myMap[strTwo].nScore += 3;
86 }
87 }
88
89 map<string, INFO>::iterator it;
90 vector<INFO> myVt;
91 for (it = myMap.begin(); it != myMap.end(); it++)
92 {
93 myVt.push_back(it->second);
94 }
95
96 sort(myVt.begin(), myVt.end());
97 for (int i = 0; i < myVt.size(); ++i)
98 {
99 printf("%s %d\n", myVt[i].strName.c_str(), myVt[i].nScore);
100 }
101 printf("\n");
102 }
103
104 return 0;
105 }