杭电1004 Let the Balloon Rise
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
This year, they decide to leave this lovely job to you.
A test case with N = 0 terminates the input and this test case is not to be processed.
green
red
blue
red
red
3
pink
orange
pink
0
#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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- hdu 1004 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 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 ...
- 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 ...
- hduoj#1004 -Let the Balloon Rise [链表解法]
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Problem Description Contest time again! How exci ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- HDU 1004 - Let the Balloon Rise(map 用法样例)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
随机推荐
- Java替换字符串中的\r\n
public static void main(String[] args) { String str = "啊\r\n啊"; str = str.replaceAll(" ...
- Django_MTV和虚拟环境
1. MVT模型 2. 虚拟环境 """ 1.安装虚拟环境的命令: 1)sudo pip install virtualenv #安装虚拟环境 2)sudo pip in ...
- HTML学习(7)格式化标签
对文本格式进行编辑的标签.常用: <b>加粗文本</b> <strong>加重语气</strong> 与<b>效果一样,<stro ...
- docker-api的使用(java)
通过docker-api来执行docker相关的操作. 配置 可以在docker启动文件docker.service中加入如下 vi /lib/systemd/system/docker.serv ...
- Vue中引入静态JS文件(爬坑)
前言(背景介绍) 开发的项目需要与Threejs的3D项目结合在一起,需要静态引入,jquery.js,stats.js,three.js,ThreeBSP.js等静态文件.开发环境是iview-ad ...
- liunx详解-2
linux安装与配置 安装配置 虚拟机配置1G内存,1核CPU,50G硬盘,网络地址转换(NAT,主机作为路由构建内网) 镜像文件:http://mirror.nsc.liu.se/centos-st ...
- CentOS7 卸载Firefox
先进入管理员模式 执行: yum remove firefox 然后用whereis 查看,却发现还是有: [root@localhost ~]# whereis firefox firefox: / ...
- Blocked Billboard II
前言 今天比赛真的状态不好(腐了一小会),导致差点爆0. 这个题解真的是在非常非常专注下写出来的,要不然真的心态崩. 刚换了域名,发现了美化脚本的bug,有点担心(汗-_-||). 题目 题目描述 奶 ...
- IIS 无法读取配置节"system.web.extensions",因为它缺少节声明
IIS 无法读取配置节"system.web.extensions",因为它缺少节声明 先安装ASP.NET 4.0 然后: 今天在本地安装iis,搭建网站,应用程序的时候报错下 ...
- 压缩/批量压缩/合并js文件
写在前面 如果文件少的话,直接去网站转化一下就行. http://tool.oschina.net/jscompress?type=3 1.压缩单个js文件 cnpm install uglify-j ...