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 - 让气球升起来的更多相关文章

  1. HDU1004 Let the Balloon Rise(map的简单用法)

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

  2. HDU1004——Let the Balloon Rise

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

  3. hdu1004 Let the Balloon Rise

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

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

  5. ACM Let the Balloon Rise

    Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the ...

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

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

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

随机推荐

  1. ios 关于时间戳与时间转化的笔记

    linux系统获取时间戳的方法:time() ; 时间戳转换成需要的时间格式: NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; [f ...

  2. MySQL5.7下修改root密码

    本地安装了个mysql5.7,发现修改root密码和以往有区别了,特此记录下 进入mysql  更改密码: mysql> update mysql.user set authentication ...

  3. Ubuntu 重装 mysql

    我另篇blog有提到修改完my.cnf文件后mysql server重新启动失败,就是说mysql server启动不起来了,于是我就想到重装再试试,没想到就好了. 重装mysql之前需要卸载干净,删 ...

  4. USACO Section 1.1-3 Friday the Thirteenth

    Friday the Thirteenth 黑色星期五 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数. 给出N年的一个 ...

  5. 用 js 的 selection range 操作选择区域内容和图片

    原创文章,转载请注明出处并保留地址.原文地址:http://www.cnblogs.com/muge10/p/6723894.html 最近在做编辑器相关的东西,遇到一个需求,用户在编辑器中插入或者粘 ...

  6. spring mvc中,直接注入的HttpServletRequst是否安全呢?

    看似很简单的一个问题,借此追踪下spring的源码处理 在写springMVC的Control中有很多这种代码, 如需要获取request对象去做某些事情 如: @Controller @Reques ...

  7. JDK源码之PriorityQueue源码剖析

    除特别注明外,本站所有文章均为原创,转载请注明地址 一.优先队列的应用 优先队列在程序开发中屡见不鲜,比如操作系统在进行进程调度时一种可行的算法是使用优先队列,当一个新的进程被fork()出来后,首先 ...

  8. Linux 基础(5)

    Linux 基础 (五) 一.shell相关知识 shell一般代表两个层面的意思,一个是命令解释器,比如BASH,另外一个就是shell脚本.通过解释器的角度来理解shel 命令分为: ==> ...

  9. Java学习记录第一章

    学习Java第一章的记录,这一章主要记录的是Java的最基础部分的了解知识,了解Java的特性和开发环境还有Java语言的优缺点. 计算机语言的发展大概过程:机器语言--->汇编语言---> ...

  10. 基于python的知乎开源爬虫 zhihu_oauth使用介绍

    今天在无意之中发现了一个知乎的开源爬虫,是基于Python的,名字叫zhihu_oauth,看了一下在github上面star数还挺多的,貌似文档也挺详细的,于是就稍微研究了一下.发现果然很好用啊.就 ...