Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 120507    Accepted Submission(s): 47270

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
 
 
 
  题目大意:这题相当于是一个配对问题,一个ball的颜色对应一个个数。
  解题思路:这题大体的解题思路是一致的,就是一种颜色要去对应一个数。然后我提供两种方法:
第一种:通过STL的map<string,int>的key-value 对应的值来求解,这里面还用到了pair转化为vector的方法,还有一些对sort排序map的扩展(MMP,就是应为这个,我以为sort(begin,end)就行,结果是不能这么玩的,必须多加一个参数)。代码如下:
#include <iostream>
#include<stdlib.h>
#include<string>
#include<map>
#include<vector>
#include<algorithm> using namespace std; typedef pair<string,size_t> PAIR; //定义排序比较函数,通过value比较
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
return lhs.second > rhs.second;
} int main(void)
{
int n;
map<string,size_t>color_balloon;
string * input_str; while(cin>>n&&n)
{
input_str=new string[n];//为输入的颜色分配空间
for(int i=;i<n;i++)
{
cin>>input_str[i];
++color_balloon[input_str[i]];//提取input_str[]计数器并对其加1
}
//把map中元素转存到vector中
vector <PAIR> color_str_vec(color_balloon.begin(),color_balloon.end());
//按降序排序
sort(color_str_vec.begin(),color_str_vec.end(),cmp_by_value);
//color_str_vec已经按照降序排序,输出第一个即为出现次数最多的一个
cout<<color_str_vec[].first<<endl;
//必须清空
color_balloon.clear();
delete [] input_str;
}
return ;
}

代码中知识参考:

思路供给:http://blog.csdn.net/always2015/article/details/44975799

map的比较函数那个知识,还有pair转化为vec的用法:http://www.cnblogs.com/fengting/p/5847347.html

第二种:

我用两个数组,一个数组用来存放字符串,一个数组用来存放对应字符串的出现次数。然后算法实现基本为:输入一个字符串,我先在之前的存入的字符串中找。如果找到了,那么该字符串对应的num++,如果找不到,那就把这个字符串作为一个新的字符串插入到字符串数组中,并且其num[i]++(从0变为1)。然后就是简单的排序输出。这个思路还是很清晰的。代码如下:

#include <iostream>
#include<math.h>
#include <iomanip>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<sstream>
#include<string.h>
using namespace std; int main()
{
int n; while(cin>>n)
{
string input[];//初始化最好在循环里弄 没那么多屁事
for(int u=;u<;u++)
input[u]=" ";
int num[]={};
string temp;
int i,j;
for(i=;i<n;i++)//开始实现算法
{
cin>>temp;//读一个字符串
for(j=;j<i;j++)//在已经输入的 字符串数组中找 有没有一样的
{
if(temp==input[j])
{
num[j]++;//有的话 对应的下标++ !!!!注意 这边是J++ 因为是在已经存在的数组中找
break;
}
}
if(i==j)//找不到 说明 没有 那就是多了一种颜色 那就要把新颜色加入到数组中去
{
input[i]=temp;
num[i]++;//然后别忘记对应的 数字++
}
} int max=;
for(i=;i<n;i++)//排序
{
if(num[i]>num[max])
{
max=i;
}
}
cout<<input[max]<<endl;//输出 } return ;
}
注意,以后没有很强的时间要求,那就把所有初始化,都放在循环中,这样就省了初始化了。

杭电1004 Let the Balloon Rise的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. hdu 1004 Let the Balloon Rise

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

  5. 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 ...

  6. 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 ...

  7. hduoj#1004 -Let the Balloon Rise [链表解法]

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Problem Description Contest time again! How exci ...

  8. HDOJ 1004 Let the Balloon Rise

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

  9. HDU 1004 - Let the Balloon Rise(map 用法样例)

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

随机推荐

  1. Java替换字符串中的\r\n

    public static void main(String[] args) { String str = "啊\r\n啊"; str = str.replaceAll(" ...

  2. Django_MTV和虚拟环境

    1. MVT模型 2. 虚拟环境 """ 1.安装虚拟环境的命令: 1)sudo pip install virtualenv #安装虚拟环境 2)sudo pip in ...

  3. HTML学习(7)格式化标签

    对文本格式进行编辑的标签.常用: <b>加粗文本</b> <strong>加重语气</strong>   与<b>效果一样,<stro ...

  4. docker-api的使用(java)

    通过docker-api来执行docker相关的操作.   配置 可以在docker启动文件docker.service中加入如下 vi /lib/systemd/system/docker.serv ...

  5. Vue中引入静态JS文件(爬坑)

    前言(背景介绍) 开发的项目需要与Threejs的3D项目结合在一起,需要静态引入,jquery.js,stats.js,three.js,ThreeBSP.js等静态文件.开发环境是iview-ad ...

  6. liunx详解-2

    linux安装与配置 安装配置 虚拟机配置1G内存,1核CPU,50G硬盘,网络地址转换(NAT,主机作为路由构建内网) 镜像文件:http://mirror.nsc.liu.se/centos-st ...

  7. CentOS7 卸载Firefox

    先进入管理员模式 执行: yum remove firefox 然后用whereis 查看,却发现还是有: [root@localhost ~]# whereis firefox firefox: / ...

  8. Blocked Billboard II

    前言 今天比赛真的状态不好(腐了一小会),导致差点爆0. 这个题解真的是在非常非常专注下写出来的,要不然真的心态崩. 刚换了域名,发现了美化脚本的bug,有点担心(汗-_-||). 题目 题目描述 奶 ...

  9. IIS 无法读取配置节"system.web.extensions",因为它缺少节声明

    IIS 无法读取配置节"system.web.extensions",因为它缺少节声明 先安装ASP.NET 4.0  然后: 今天在本地安装iis,搭建网站,应用程序的时候报错下 ...

  10. 压缩/批量压缩/合并js文件

    写在前面 如果文件少的话,直接去网站转化一下就行. http://tool.oschina.net/jscompress?type=3 1.压缩单个js文件 cnpm install uglify-j ...