24h購物| | PChome| 登入
2012-07-04 21:02:03| 人氣1,703| 回應0 | 上一篇 | 下一篇

[UVA][map&set混用] 11239 - Open Source

推薦 0 收藏 0 轉貼0 訂閱站台

Problem A: Open Source

At an open-source fair held at a major university, leaders of open-source projects put sign-up sheets on the wall, with the project name at the top in capital letters for identification.

Students then signed up for projects using their userids. A userid is a string of lower-case letters and numbers starting with a letter.

The organizer then took all the sheets off the wall and typed in the information.

Your job is to summarize the number of students who have signed up for each project. Some students were overly enthusiastic and put their name down several times for the same project. That's okay, but they should only count once. Students were asked to commit to a single project, so any student who has signed up for more than one project should not count for any project.

There are at most 10,000 students at the university, and at most 100 projects were advertised.

Input Specification:

The input contains several test cases, each one ending with a line that starts with the digit 1. The last test case is followed by a line starting with the digit 0.

Each test case consists of one or more project sheets. A project sheet consists of a line containing the project name in capital letters, followed by the userids of students, one per line.

Output Specification:

For each test case, output a summary of each project sheet. The summary is one line with the name of the project followed by the number of students who signed up. These lines should be printed in decreasing order of number of signups. If two or more projects have the same number of signups, they should be listed in alphabetical order.

Sample input

UBQTS TXT
tthumb
LIVESPACE BLOGJAM
philton
aeinstein
YOUBOOK
j97lee
sswxyzy
j97lee
aeinstein
SKINUX
1
0

Output for Sample Input

YOUBOOK 2
LIVESPACE BLOGJAM 1
UBQTS TXT 1
SKINUX 0


題目不難理解, 但要做起來又十分麻煩, 而且輸出又要排序,
真是把 STL 一次複習了好多東西

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
struct D {
string s;
int n;
};
bool cmp(D i, D j) {
if(i.n != j.n)
return i.n > j.n;
return i.s.compare(j.s) < 0;
}
int main() {
while(1) {
string proj, name;
map<string, string> stu;
map<string, set<string> > pro;
getline(cin, proj);
stu[""] = "-1";
while(1) {
if(proj[0] == '0')
return 0;
if(proj[0] == '1')
break;
pro[proj].insert("");
while(getline(cin, name)) {
if(name[0] >= 'A' && name[0] <= 'Z' || name[0] == '1') {
proj = name;
break;
}
if(stu[name] == "" || stu[name] == proj) {
stu[name] = proj;
pro[proj].insert(name);
} else {
stu[name] = "-1";
}
}
}
int idx = 0;
D ans[100];
for(map<string, set<string> >::iterator i = pro.begin(); i != pro.end(); i++) {
//cout << i->first << endl;
int cnt = 0;
for(set<string>::iterator j = i->second.begin(); j != i->second.end(); j++) {
//cout << *j << endl;
if(stu[*j] != "-1")
cnt++;
}
ans[idx].s = i->first;
ans[idx].n = cnt;
idx++;
}
sort(ans, ans+idx, cmp);
for(int i = 0; i < idx; i++)
cout << ans[i].s << " " << ans[i].n << endl;
}
return 0;
}

台長: Morris
人氣(1,703) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 442 - Matrix Chain Multiplication
此分類上一篇:[UVA] 400 - Unix ls

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文