码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 1004 Let the Balloon Rise (字符串+stl)

时间:2018-11-07 00:08:35      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:容器   数组   string   tput   转换   sig   颜色   inpu   div   

题目:
Problem Description
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

题意:给出每个气球的颜色 统计出现次数最多的颜色
思路:网上的代码大多数都是开两个数组来记录 自己的方法是用stl中的map函数来做 用map计数然后排序 但是map并不是线性的 不能用sort 需要转换

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
int n;
string s;

void value(vector<pair<int,string>> & vec,map<string,int> & mp){
    for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){
        vec.push_back(make_pair(it->second,it->first));
    }
    sort(vec.begin(),vec.end());
}

int main(){
    while(~scanf("%d",&n)){
        if(n==0) break;
        vector<pair<int,string>> vec;
        map<string,int>mp;
        while(n--){
            cin>>s;
            mp[s]++;
        }
        value(vec,mp);
        vector<pair<int,string>>::iterator it=vec.end()-1;
        cout<<it->second.c_str()<<endl;
    }
    return 0;
}

题外话:关于排序

  sort算法只能对线性的容器进行排序 例如vector list deque 但是map是一个集合容器 里面存储的元素是pair 内部是按序存储的 比如红黑树 如此就不能用sort进行排序 为了解决这个问题 可以把map中的元素放入到vector 从而进行排序操作

 
 

 

HDOJ 1004 Let the Balloon Rise (字符串+stl)

标签:容器   数组   string   tput   转换   sig   颜色   inpu   div   

原文地址:https://www.cnblogs.com/whdsunny/p/9919766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!