hdu 1004 map和非map写法

Let the Balloon Rise

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

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

用map非常简单,不过鉴于这道题本身很水,不用map也差不了太多

map

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<set>
 7 #include<vector>
 8 #include<map>
 9
10 using namespace std;
11
12 int main()
13 {
14     int n;
15     string co,ss;
16     map<string ,int>color;
17     while(cin>>n,n)
18     {
19         color.clear();
20         for(int i = 1; i <= n; i++)
21         {
22             cin>>co;
23             color[co]++;
24         }
25         int max = 0;
26         for(map<string,int>::iterator iter = color.begin();iter!=color.end();iter++)
27         {
28             if(iter->second>max)
29             {
30                 max = iter->second;
31                 ss = iter->first;
32             }
33         }
34         cout<<ss<<endl;
35
36     }
37     return 0;
38 }

非map

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     int n,i,j,max;
 6     char a[1010][18];
 7     while(scanf("%d",&n),n)
 8     {
 9         max=1;
10         int b[1005];
11         for(i=1;i<=n;i++)
12         {
13              scanf("%s",&a[i]);
14              b[i]=1;
15         }
16         for(i=1;i<n;i++)
17         {
18             if(b[i]==0)
19                 continue;
20             for(j=i+1;j<=n;j++)
21             {
22                 if(strcmp(a[i],a[j])==0)
23                 {
24                     b[i]++;
25                     b[j]=0;
26                 }
27             }
28         }
29         for(i=2;i<=n;i++)
30             if(b[max]<b[i])
31                 max=i;
32         printf("%s\n",a[max]);
33
34     }
35 }
时间: 2024-10-12 23:27:34

hdu 1004 map和非map写法的相关文章

hdu 4941 Magical Forest (map容器)

Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 135    Accepted Submission(s): 69 Problem Description There is a forest can be seen as N * M grid. In this forest, there is so

HDU Bombing (STL multiset+map)

题意:给你 n 个坐标(x,y),m 个询问(c,d) c==0,求出x==d有多少个,并删除这些点: c==1,求出y==d有多少个,并删除这些点. map+multiset的多重映射 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream>

hdu 4941 Magical Forest ( 双重map )

题目链接 题意: 有一个n*m的田地,里边有k棵树,每棵树的位置为(xi,yi),含有能量值ci.之后又q个询问,分三种; 1)1 a b,将a行和b行交换 2)2 a b,将a列和b列交换 3)3 a b,询问(a,b)位置的果树的能量值. 分析: hdu 4941 Magical Forest ( 双重map )

HDU 1247 Hat&#39;s Words (map+string)

Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10993    Accepted Submission(s): 3944 Problem Description A hat’s word is a word in the dictionary that is the concatenation of exact

HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第一种是行交换操作,就是把矩阵的两行进行交换,另一种是列交换操作,注意两种操作都要求行或列至少要有一个水果,第三种操作是查找,询问第A行B列的水果的能量值,如果查询的位置没有水果,则输出0. 因为n和m都很大,达到了2*10^9,但水果最多一共只有10^5个,我的做法是直接用结构体存了之后排序,然后m

HDU 1004

第一次写map 纪念 1 #include <map> 2 #include <cstdio> 3 #include <iostream> 4 #include <string> 5 #include <cstring> 6 #include <string.h> 7 using namespace std; 8 map<string,int> a; 9 int main(){ 10 int t,max1; 11 stri

mybatis异常:Could not find result map java.util.Map 问题分析及解决

错误写法 <select id="queryXXXCount" resultMap="java.util.Map" > mybatis报出的异常日志: org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.util.Map at org.apache.ibatis.builder.MapperBuilderAssistant.setStat

另一种遍历Map的方式: Map.Entry 和 Map.entrySet()

源网址: http://blog.csdn.net/mageshuai/article/details/3523116 今天看Think in java 的GUI这一章的时候,里面的TextArea这个例子在遍历Map时用到了Map.Entry 和 Map.entrySet() ,记得只见过Map.KeySet()和values()这两个方法,于是到API中一看,Map.entrySet() 这个方法返回的是一个Set<Map.Entry<K,V>>,Map.Entry 是一个接口

[转]另一种遍历Map的方式: Map.Entry 和 Map.entrySet()

转自: http://blog.csdn.net/mageshuai/article/details/3523116 今天看Think in java 的GUI这一章的时候,里面的TextArea这个例子在遍历Map时用到了Map.Entry 和 Map.entrySet() ,记得只见过Map.KeySet()和values()这两个方法,于是到API中一看,Map.entrySet() 这个方法返回的是一个Set<Map.Entry<K,V>>,Map.Entry 是一个接口,