[HDU1004] Let the balloon rise - 让气球升起来
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.
又是比赛时间!看见气球漂浮着是多么激动人心啊。但是告诉你一个秘密,裁判最喜欢的时间是才最受欢迎的问题。当比赛结束时,他们会数出每种颜色的气球然后得出结果。
今年,他们决定把这项可爱的工作留给你。
(注:好像ACM的标志就是气球)
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.
输入包含多组测试数据。每组数据以N(0 < N <= 1000)开始——放飞的气球总数。下面的N行各描述一个颜色。一个气球的颜色是一个最多有15个小写字母的字符串。
一组N为0的测试数据表示停止输入,并且这组数据不应被处理。
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.
对于每一组数据,使用一整行输出最热门的问题的气球的颜色。保证每组问题的解是唯一的。
分析
输入N个字符串,求出现次数最多的字符串。方法多种多样。
但是要注意:
1. N的范围是0<N<=1000,因此N也可以是1或2,如果是这样可以直接输出任意一个颜色(都是一样的)。因为N=1时,输入的就是出现最多的;N=2时,因为有唯一解,所以任意选一个输出。否则容易造成access violation。
2. 如果用char数组存储字符串,注意数组大小是[1000][16]而不是[16][1000],否则100%会造成access violation。
方法1
先输入全部字符串,然后按照字典顺序排序(C用qsort(),C++用sort())。从第一个一直检查到最后一个,看哪个出现的最多。
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int cmp(const void *a, const void *b); int main()
{
int n;
char c[][]; // Not c[16][1000]!!!
while (scanf("%d", &n), n != )
{
int i;
for (i = ; i < n; i++)
scanf("%s", c[i]);
if (n == || n == )
{
printf("%s\n", c[]);
continue;
}
qsort(c, n, sizeof(c[]), cmp);
int ct = ;
int mx = ;
char * p;
for (i = ; i < n; i++)
{
if (strcmp(c[i], c[i - ]) == ) // Use strcmp() instead of == to compare two strings
ct++;
else
ct = ;
if (ct > mx)
{
mx = ct;
p = c[i];
}
}
printf("%s\n", p);
}
return ;
} int cmp(const void *a, const void *b)
{
return strcmp((char *)a, (char *)b);
}
P.S. 有个奇怪的地方。这样写就可以:
int n;
while (scanf("%d", &n), n != )
{
...
}
这样就超时:
int n;
scanf("%d", &n);
while (n != )
{
...
scanf("%d", &n);
}
用时:0ms
C++
C++中我就用了string和sort()进行排序。和C语言大体上是相似的。
#include <iostream>
#include <string>
#include <algorithm> int main()
{
using namespace std; int n;
string c[]; ios_base::sync_with_stdio(false); while (cin>>n, n!=)
{
int i;
for (i = ; i < n; i++)
cin >> c[i];
if (n == || n == )
{
cout << c[] << endl;
continue;
}
sort(c, c + n);
int ct = ;
int mx = ;
string & best = c[];
for (i = ; i < n; i++)
{
if (c[i] == c[i - ])
ct++;
else
ct = ;
if (ct > mx)
{
mx = ct;
best = c[i];
}
}
cout << best << endl;
} return ;
}
用时:15ms
方法2
先输入全部的字符串,然后对所有字符串,判断该字符串后面有几个与其相同的(也可以是前面)。数量最多的就是答案。
C
#include <stdio.h>
#include <string.h> int main()
{
int n;
char c[][];
int t[]; while (scanf("%d", &n), n != )
{
int i, j;
for (i = ; i < n; i++)
scanf("%s", c[i]);
for (i = ; i < n; i++)
t[i] = ;
int mx = ;
int best;
for (i = ; i < n - ; i++)
{
for (j = i + ; j < n; j++)
if (strcmp(c[i], c[j]) == )
t[i]++;
if (t[i] > mx)
{
mx = t[i];
best = i;
}
}
printf("%s\n", c[best]);
} return ;
}
用时:0ms
C++
#include <iostream>
#include <string>
#include <algorithm> int main()
{
using namespace std; int n;
string c[];
int t[]; ios_base::sync_with_stdio(false); while (cin >> n, n != )
{
int i, j;
for (i = ; i < n; i++)
cin >> c[i];
fill(t, t + n, ); // set all elements of t[] to 0
int mx = ;
int best;
for (i = ; i < n - ; i++)
{
for (j = i + ; j < n; j++)
if (c[i] == c[j])
t[i]++;
if (t[i] > mx)
{
mx = t[i];
best = i;
}
}
cout << c[best] << endl;
} return ;
}
用时:15ms
方法3
使用map/Map存储一个字符串出现的次数,出现最多的就是答案。注意C++中如果使用map,最好用string存储字符串。
C++
#include <iostream>
#include <map>
#include <string>
int main()
{
using namespace std; map<string, int> col;
int n; ios_base::sync_with_stdio(false); while (cin >> n, n != )
{
string s, p;
int mx = ;
for (int i = ; i < n; i++)
{
cin >> s;
col[s]++;
if (mp[s] > mx)
{
mx = col[s];
p = s;
}
}
cout << p << endl;
} return ;
}
用时:0ms
如果代码可以更好,可以在评论中指出!
注:本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6792144.html
[HDU1004] Let the balloon rise - 让气球升起来的更多相关文章
- HDU1004 Let the Balloon Rise(map的简单用法)
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1004——Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- hdu1004 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【STL<map>】
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- ACM Let the Balloon Rise
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the ...
- STL-map-A - Let the Balloon Rise
A - Let the Balloon Rise Contest time again! How excited it is to see balloons floating around. But ...
- hdu 1004 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- Let the Balloon Rise 分类: HDU 2015-06-19 19:11 7人阅读 评论(0) 收藏
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 ...
随机推荐
- 如何精准高效的实现视觉稿?------前端开发辅助工具AlloyDesigner使用介绍
AlloyDesigner:http://alloyteam.github.io/AlloyDesigner/ 介绍:AlloyDesigner是腾讯开发的一款工具,其在页面构建过程中,直接嵌入开发的 ...
- EasyUI之Hello world(EasyUI的入门学习)
1:创建一个动态web工程: 去官网http://www.jeasyui.net/download/下载官网文档 我去官网下载的最新版本,个人根据自己的需求下载即可.2:在webConte ...
- 用react开发一个新闻列表网站(PC和移动端)
最近在学习react,试着做了一个新闻类的网站,结合ant design框架, 并且可以同时在PC和移动端运行: 主要包含登录和注册组件.头部和脚部组件.新闻块类组件.详情页组件.评论和收藏组件等: ...
- 烧录口被初始化为普通IO
烧录口被初始化为普通IO后如果复位端没有的烧录口会导致不能识别烧录器不能下载与调试,因为程序一开始就把端口初始化了,烧录器不能识别,添加复位端口到烧录器(前提是你的烧录器有复位端). 有了复位段之后, ...
- Python 基础 一
Python 基础 一 一.关于Python的介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum),这一两年在国内很流行,应用很广泛. 二.Python的基础知识(1) 1 ...
- .NET 二维码生成(ThoughtWorks.QRCode)【转发jiangys】
.NET 二维码生成(ThoughtWorks.QRCode) 2015-06-21 22:19 by jiangys, 3790 阅读, 8 评论, 收藏, 编辑 引用ThoughtWorks.QR ...
- java并发程序——并发容器
概述 java cocurrent包提供了很多并发容器,在提供并发控制的前提下,通过优化,提升性能.本文主要讨论常见的并发容器的实现机制和绝妙之处,但并不会对所有实现细节面面俱到. 为什么JUC需要提 ...
- 【2017-05-02】winform弹出警告框是否进行增删改操作、记事本制作、对话框控件和输出输入流
一.winform弹出警告框是否进行增删改操作 第一个参数是弹出窗体显示的内容,第二个参数是标题,第三个参数是该弹窗包含确定和取消按钮. 返回的是一个枚举类接收一下. 再进行判断,如果点的是确定按钮, ...
- Redis 基本安全规范文档
温馨提示:我在一家手游的公司工作,因为经常用到redis,特为此整理文档(借鉴过大神的文章): 一.什么是redis(出自百度百科)? redis是一个key-value存储系统.和Memcached ...
- java入门基础
什么是java? java是一门编程语言 编程语言有很多种 你比如 C语言 等等 为什么学习java呢! 因为你要和计算机交互 当然了你用汉语跟她说她听不懂 所以你要学习编程语言 那么额咱们的ja ...