国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 南涧| 诏安县| 噶尔县| 丘北县| 巴南区| 大英县| 周口市| 洪雅县| 赤水市| 诸城市| 平顺县| 铜陵市| 吴堡县| 闽侯县| 延庆县| 集安市| 康马县| 喀喇沁旗| 历史| 天峻县| 彭州市| 甘洛县| 防城港市| 乐安县| 泗洪县| 辽宁省| 杭锦后旗| 东乡族自治县| 界首市| 资源县| 区。| 舞钢市| 桂平市| 淮滨县| 新干县| 宁武县| 开封县| 应城市| 日土县| 罗甸县| 大化|