1230: Open Source
Time Limit: 1 Sec Memory Limit: 128 MB[ Submit][ Status][ Web Board]
Description
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.
Input
Please Input Input Here
Output
Please Input Output Here
Sample Input
Sample Output
【分析】
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
map<string,int> name;
map<string,set<int> > f;
map<int,string> work;
struct xx{
string x;
int y;
}a[10000];
int n,len;
int cmp(xx p,xx q)
{
if (p.y==q.y) return p.x<q.x;
return p.y>q.y;
}
void write()
{
std::map<string, set<int> >::iterator it;
int t=0;
for (it=f.begin();it!=f.end();it++)
{
// cout<<"****"<<it->first;
int res=0;
a[t].x=it->first;
std::set<int>::iterator is;
for (is=it->second.begin();is!=it->second.end();is++)
{
// cout<<"***"<< *is;
if (*is!=-1 && name[work[*is]]!=-1)
res++;
}
// cout<<endl;
a[t].x=it->first;
a[t].y=res;
t++;
}
sort(a,a+t,cmp);
for (int i=0;i<t;i++)
cout<<a[i].x<<" "<<a[i].y<<endl;
}
int main()
{
string s;
string now;
len=1;
name.clear();
f.clear();
work.clear();
while (getline(cin,s))
{
if (s=="0") break;
if (s=="1")
{
write();
len=1;
name.clear();
f.clear();
work.clear();
}
else
if (s[0]>='A' && s[0]<='Z')
{
now=s;
f[now].insert(-1);
}
else
if (name[s]!=-1)
{
int tt=0;
if (!name[s]) name[s]=len,work[len]=s,len++;else tt=1;
std::set<int>::iterator t=f[now].find(name[s]);
if (t==f[now].end() && tt)
{
name[s]=-1;
continue;
}
f[now].insert(name[s]);
}
}
return 0;
}