STL的应用

HDU-Let the Balloon Rise

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.

Output

For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.

Sample Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink

题意:每次给出一个n,接下来n个颜色,统计出现次数最多的那个颜色。

解析:用map存一下

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
map<string,int>q;
int main()
{
    int n;
    while(~scanf("%d ",&n)&&n)
    {
        q.clear();
        while(n--)
        {
            string s;
            cin>>s;
            q[s] ++;
        }
        map<string,int>::iterator it;
        int maxx = 0;
        string res = "";
        for(it = q.begin(); it !=  q.end(); it ++)
            if(it->second>maxx)
            {
                maxx = it->second;
                res = "";
                res += it->first;
            }
        cout<<res<<endl;
            }

}

 

度熊所居住的 D 国,是一个完全尊重人权的国度。以至于这个国家的所有人命名自己的名字都非常奇怪。一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字。例如,如果一个人名字是 ACM,那么 AMC, CAM, MAC, MCA, 等也都是这个人的名字。在这个国家中,没有两个名字相同的人。

度熊想统计这个国家的人口数量,请帮助度熊设计一个程序,用来统计每一个人在之前被统计过多少次。

Input

这里包括一组测试数据,第一行包含一个正整数N

,接下来的N 行代表了 N 个名字。N 不会超过100,000

,他们的名字不会超过40位.

Output

对于每输入的一个人名,输出一个整数,代表这个人之前被统计了多少次。

Sample Input

5
ACM
MAC
BBA
ACM
BAB

Sample Output

0
1
0
2
1

解析:先用sort排一下序,然后用map存一下就好了~

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
map<string,int>q;
int cmp(string a,string b)
{
    return a<b;
}
int main()
{
    int n;
    scanf("%d ",&n);
    while(n--)
    {
        char s[1005];
        cin>>s;
        int len = strlen(s);
        sort(s,s+len);
        printf("%d\n",q[s]++);
    }
}

给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.

Input

每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.

Output

针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.

Sample Input

1 2
1
2 3
1 2
1
1 2

Sample Output

1 2 3
1 2

解析:利用set的去重功能

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<set>
using namespace std;
set<int>a;
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        a.clear();
        int sum = n + m;
        for(int i = 0; i < sum; i ++)
        {
            int x;
            scanf("%d",&x);
            a.insert(x);
        }
        set<int>::iterator it;
        for(it = a.begin(); it != a.end(); it ++)
        {
            if(it==a.begin())
                printf("%d",*it);
            else
                printf(" %d",*it);
        }
        printf("\n");
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值