杭电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

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=0;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[0].first<<endl;
        //必须清空
        color_balloon.clear();
        delete [] input_str;
    }
    return 0;
}

代码中知识参考:

思路供给: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[1002];//初始化最好在循环里弄 没那么多屁事
    for(int u=0;u<1002;u++)
        input[u]=" ";
    int num[1002]={0};
        string temp;
        int i,j;
        for(i=0;i<n;i++)//开始实现算法
        {
            cin>>temp;//读一个字符串
            for(j=0;j<i;j++)//在已经输入的 字符串数组中找 有没有一样的
            {
                if(temp==input[j])
                {
                    num[j]++;//有的话 对应的下标++ !!!!注意 这边是J++ 因为是在已经存在的数组中找
                    break;
                }
            }
            if(i==j)//找不到 说明 没有 那就是多了一种颜色 那就要把新颜色加入到数组中去
            {
                input[i]=temp;
                num[i]++;//然后别忘记对应的 数字++
            }
        }

        int max=0;
        for(i=0;i<n;i++)//排序
        {
            if(num[i]>num[max])
            {
                max=i;
            }
        }
        cout<<input[max]<<endl;//输出

    }

    return 0;
}

注意,以后没有很强的时间要求,那就把所有初始化,都放在循环中,这样就省了初始化了。

时间: 2024-10-22 07:11:04

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

HDU 1004 Let the Balloon Rise【STL&lt;map&gt;】

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 123800    Accepted Submission(s): 48826 Problem Description Contest time again! How excited it is to see balloons floating ar

hdoj 1004 Let the Balloon Rise(模拟 || 字典树)

Let the Balloon Rise http://acm.hdu.edu.cn/showproblem.php?pid=1004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84401    Accepted Submission(s): 31831 Problem Description Contest time again

hdu 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): 80469    Accepted Submission(s): 30293 Problem Description Contest time again! How excited it is to see balloons floating aro

HDU 1004 Let the Balloon Rise map

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 104108    Accepted Submission(s): 40046 Problem Description Contest time again! How excited it is to see balloons floating ar

HDU 1004 Let the Balloon Rise (map使用)

分析:题意简单,就用map的知识即可 #include <stdio.h> #include <iostream> #include <string.h> #include <map> using namespace std; int main() { int i,a,b,k; int N; char num[10]; while (~scanf("%d",&N)) { map<string,int> mp; whil

HDOJ 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): 138223    Accepted Submission(s): 54591 Problem Description Contest time again! How excited it is to see balloons floating ar

HDOJ 1004 Let the Balloon Rise (字符串+stl)

题目: 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 co

啊啊啊啊啊啊啊啊啊啊啊真是刷了一道有意义的题哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 杭电1004

输入字符串,并比较输入频率最大的那个字符串 #include<iostream> using namespace std; int main( ) { int round; cin >> round; while (round) { char ballo[1001][16]; int fre[1001] = { 0}; char tem[15]; int m = 0, n = 0; int count = 0; for (int i = 0; i < round; i++)

水题/hdu 1004 Let the Balloon Rise

题意 给出n个字符串,输出出现次数最多的那个 分析 存下字符串后排序,再统计,输出 Accepted Code 1 /* 2 PROBLEM:hdu1004 3 AUTHER:Nicole Lam 4 MEMO:水题 5 */ 6 7 #include<iostream> 8 #include<cstring> 9 #include<string> 10 #include<algorithm> 11 using namespace std; 12 13 in