题目:
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==) 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()-;
cout<<it->second.c_str()<<endl;
}
return ;
}

题外话:关于排序

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

 
 

HDOJ 1004 Let the Balloon Rise (字符串+stl)的更多相关文章

  1. HDU 1004 Let the Balloon Rise【STL<map>】

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  3. HDU 1004 Let the Balloon Rise(STL初体验之map)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  4. hdu 1004 Let the Balloon Rise(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...

  5. HDU 1004 Let the Balloon Rise(map的使用)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...

  6. 杭电1004 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  7. hdu 1004 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. HDU 1004 Let the Balloon Rise map

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. hdu 1004 Let the Balloon Rise strcmp、map、trie树

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. java开发常用的日期格式转换工具类

    package com.ydtf.ipcc.sms.util; import java.sql.Timestamp; import java.text.SimpleDateFormat; import ...

  2. vue init 解决办法

    /由于windows系统的某方面问题,vue脚手架安装可能会出现第一证书丢失 // 报错:vue-cli · Failed to download repo vuejs-templates/webpa ...

  3. MySQL_参数设置

    1.max_allowed_packet 描述:有时备份数据库时,将本地psc文件备份到数据库时,会遇见备份不成功的情况 分析:这时要考虑,是否由于max_allowed_packet这个参数过小,导 ...

  4. MyBatis-Helloworld

    一.依赖配置 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  5. 13.模板方法(Template Method)

    无处不在的Template Method     如果你只想掌握一种设计模式,那么它就是Template Method!动机(Motivate):    变化 -----是软件设计的永恒主题,如何管理 ...

  6. vs code配置git

    在项目目录执行 git init 修改.git文件夹下的config文件 [core] repositoryformatversion = 0 filemode = false bare = fals ...

  7. [leetcode-128] 最长连续序列

    给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, ...

  8. Java中的AES加解密

    直接上代码,Base64使用的是Java8的方法,如没有,替换即可 KEY:即密码 IV:即偏移量,可自订,十六位 加密方式:AES/CBC/PKCS5Padding,128位加密 如果想用256位和 ...

  9. docker 系列 - Docker CheatSheet | Docker 配置与实践清单 (转载)

    本文转载自 (https://segmentfault.com/a/1190000016447161), 感谢作者.

  10. SpringBoot系列: 理解 Spring 的依赖注入(二)

    ==============================Spring 容器中 Bean 的名称==============================声明 bean 有两个方式, 一个是 @B ...