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.
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
Author
WU, Jiazhi
Source
题目类型:map/Trie树
算法分析:直接建立一个映射字符串的数据结构,然后统计次数即可,这里分别使用STL中的map和Trie树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 60000 + 66; int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int n; while (cin >> n) { if (n == 0) break; map <string, int> cnt; string s; int maxval = -INF; string res; for (int i = 1; i<= n; i++) { cin >> s; cnt[s]++; if (maxval < cnt[s]) { maxval = cnt[s]; res = s; } } cout << res << endl; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 26; const int nodemaxn = 15000; int maxval; struct Trie { int next[nodemaxn][maxn], last[nodemaxn], cnt[nodemaxn]; int root , len; void Init () { len = 0; root = newnode (); } int newnode () { for (int i = 0; i < maxn; i++) next[len][i] = -1; last[len] = 0, cnt[len] = 0; len++; return len - 1; } void Insert (char *s) { int p = root; for(int i = 0; s[i]; i++) { if(next[p][s[i]-'a'] == -1) next[p][s[i]-'a'] = newnode (); else { p = next[p][s[i]-'a']; } } last[p] = 1; cnt[p]++; } int Query (char *s) { if (!s[0]) return 0; int p = root; for (int i = 0; s[i]; i++) { if (next[p][s[i]-'a'] == -1) return 0; p = next[p][s[i]-'a']; } if (last[p]) return cnt[p]; } }; Trie aa; char ss[1111][maxn]; int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int n; while (cin >> n) { if (n == 0) break; aa.Init (); for (int i = 1; i <= n; i++) { cin >> ss[i]; aa.Insert (ss[i]); } maxval = -INF; char sss[maxn]; for (int i = 1; i <= n; i++) { if (maxval < aa.Query(ss[i])) { maxval = aa.Query (ss[i]); strcpy(sss, ss[i]); } } cout << sss << endl; } return 0; } |
- « 上一篇:hdu1003
- hdu1005:下一篇 »