国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Let the Balloon Rise hdu1001 字典樹

2019-11-08 01:43:19
字體:
供稿:網(wǎng)友

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.

Author


WU, Jiazhi

Source


ZJCPC2004

Solution


嗯,接觸的第一道字典樹題目 gdkoi被虐以后想了很多,看來還是努力比較實在,就從頭開始了

字典樹顧名思義就是一棵樹,它以字母也不僅僅以字母作為節(jié)點,相比哈希能節(jié)省較多的空間,然后查找的復雜度只與查詢字符串的長度有關

題目要求找出現(xiàn)最多的單詞,那么開一棵trie然后加單詞就行了,加完所有單詞遍歷一下。其實也是可以插入的同時查詢最大值的,跑得賊快

Code


#include <iostream>#include <string.h>#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)#define fill(x, t) memset(x, t, sizeof(x))#define N 5001#define L 131using namespace std;int map[N][L], p[N], cnt = 0, ans;string prt;inline void insert(int now, string s, int dep){ if (dep == s.length()){ p[now] += 1; if (p[now] > ans){ ans = p[now]; prt = s; } return; } if (!map[now][s[dep]]){ map[now][s[dep]] = ++ cnt; } insert(map[now][s[dep]], s, dep + 1);}int main(void){ ios::sync_with_stdio(false); int n; while (cin >> n && n){ ans = 0; cnt = 0; fill(map, 0); fill(p, 0); rep(i, 1, n){ string s; cin >> s; insert(0, s, 0); } cout << prt << endl; } return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 建宁县| 六枝特区| 桑植县| 湘西| 莱西市| 张家界市| 栖霞市| 永福县| 平潭县| 林西县| 温泉县| 辰溪县| 商都县| 石门县| 五河县| 浠水县| 锦屏县| 府谷县| 绥德县| 象州县| 文昌市| 来凤县| 松原市| 福贡县| 城口县| 秭归县| 阿坝| 永顺县| 星座| 桃江县| 平乐县| 金平| 曲麻莱县| 岗巴县| 灵山县| 五指山市| 南充市| 翁源县| 繁昌县| 桂东县| 崇礼县|