题目:
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. python: 反射机制;

    import comma def run(): inp = input('请输入要调用的函数').strip(); if hasattr(comma,inp): fun = getattr(comma ...

  2. sklearn—特征工程

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  3. java 中+的运算规则

    1.Java中的加法的运算优先级是从左往右的 2.字符串""隔壁跟的+号意思是字符串的连接 就不是加法了 3.'字符' 后面的+号意思是'字符'的ascall码值和后面的值相加 c ...

  4. Kubernetes之POD

    什么是Pod Pod是可以创建和管理Kubernetes计算的最小可部署单元.一个Pod代表着集群中运行的一个进程. Pod就像是豌豆荚一样,它由一个或者多个容器组成(例如Docker容器),它们共享 ...

  5. 解决phpmyadmin 遇见的问题

    1.phpmyadmin4.8.3 上传到网站目录后提示解决phpmyadmin mysqli_real_connect(): (HY000/2002): No such file or direct ...

  6. Kafka技术内幕 读书笔记之(三) 消费者:高级API和低级API——消费者消费消息和提交分区偏移量

    消费者拉取钱程拉取每个分区的数据,会将分区的消息集包装成一个数据块( FetchedDataChunk )放入分区信息的队列中 . 而每个队列都对应一个消息流( KafkaStream ),消费者客户 ...

  7. JAVA-String,StringBuilder,StringBuffer例子(基础必备)

    package com.net.xinfang.reflect; import java.io.UnsupportedEncodingException; /** * String不属于8种基本数据类 ...

  8. Junit4学习与使用【转】

    参考: http://blog.csdn.net/qqhjqs/article/details/42219037

  9. OpenStack配置串口显示虚机界面

    OpenStack配置串口显示虚机界面 OpenStack的horizon能够显示虚拟机的界面.horizon是web界面,在我们的电脑上,姑且称之为本地,虚拟机运行在远端服务器上,称之为远端.本地显 ...

  10. dubbo的具体使用

    dubbo的具体使用: dubbo简介: 1.1.    什么是dubbo 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,急需一个治理 ...