TOJ 2494: Open Source Map使用

2494: Open Source 分享至QQ空间
时间限制(普通/Java):1000MS/10000MS 内存限制:65536KByte
总提交: 3 测试通过:2
描述

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.

输入

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.

输出

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.

样例输入

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

样例输出

YOUBOOK 2
LIVESPACE BLOGJAM 1
UBQTS TXT 1
SKINUX 0

题目来源

Waterloo July.14 2007
题解:这题map即可解决,题意是学校有很多个项目,每个人都仅能申报一个项目,如果一个人报了多个项目,那么都不算,大写的是项目,小写的是同学,输出每个项目和有效的同学数量,数量多的先,数量一样按项目名字典排序。

#include<bits/stdc++.h>
using namespace std;
map<string,int>net; // 标记没有放过
map<string,int>vis;	//	标记是否已经减去该人了
struct edge{
    string name;
    vector<string>son;
    int ans;
};
bool cmp (edge a,edge b){ //排序
    if(a.ans!=b.ans) return a.ans>b.ans;
    return a.name<b.name;
}
int main(){
    string s;
    int cnt = 0;
    edge e[105];
    while(getline(cin,s) && s!="0"){ //注意输入有空格
        if(s=="1"){ // 输出 初始化
            sort(e+1,e+cnt+1,cmp);
            for(int i=1;i<=cnt;i++){
                cout << e[i].name << " " << e[i].ans << endl;
                e[i].son.clear();
            }
            cnt = 0;
            net.clear(),vis.clear();
        }
        if(s[0]>='A'&&s[0]<='Z') e[++cnt].name = s,e[cnt].ans=0; // 项目新添
        else{
            if(!net[s]){ // 判断是否存在 ,不存在新添到该项目中
                e[cnt].son.push_back(s);
                net[s]=cnt;
                e[cnt].ans++;
            }
            else{ 
                if(net[s] == cnt) continue; // 特判是不是一个人在同一个项目写了两次
                if(!vis[s]){ // 判断是否减去过该人了
                    e[net[s]].ans--;
                    vis[s]=1;
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值