标签:int 实现 nod others nyoj code 计数 return clu
10 boar pig sheep gazelle sheep sheep alpaca alpaca marmot mole
sheep 3
/** PS:这道题第一反应就是用map但用map提交两次都超时?????? 数据结构:字典树 **/
模板 struct node、 void my_insert(char *s)、 root:
1 struct node 2 { 3 node *next [26]; 4 int cnt; 5 node () 6 { 7 cnt = 0; 8 memset (next, 0, sizeof (next)); 9 } 10 }; 11 12 node *root = new node (); 13 14 void my_insert (char *s) 15 { 16 node *p = root; 17 int i, k, len = strlen (s); 18 for (i = 0; i < len; ++ i) 19 { 20 k = s [i] - ‘a‘; 21 if (p->next [k] == NULL) 22 p->next [k] = new node (); 23 p = p->next [k]; 24 } 25 p->cnt ++; 26 if (p->cnt > my_max) 27 { 28 my_max = p->cnt; 29 strcpy (ans, s); 30 } 31 }
C/C++代码实现(AC):
#include <bits/stdc++.h> using namespace std; int n, my_max = -1; char temp [15], ans [15]; struct node { node *next [26]; int cnt; node () { memset (next, 0, sizeof (next)); cnt = 0; } }; node *root = new node(); // new 与 malloc 类似都是开辟内存空间的意思 void my_resert (char *s) { node *p = root; int i, k, len = strlen (s); for (i = 0; i < len; ++ i) { k = s [i] - ‘a‘; if (p->next [k] == NULL) p->next [k] = new node(); p = p->next [k]; } p->cnt ++; if (p->cnt > my_max) { my_max = p->cnt; strcpy (ans, s); } return ; } int main() { scanf ("%d", &n); while (n --) { scanf ("%s", temp); my_resert (temp); } printf ("%s %d", ans, my_max); return 0; }
hdu 1251 统计难题 http://acm.hdu.edu.cn/showproblem.php?pid=1251
nyoj 290 动物统计加强版 (字典树 PS:map<TLE>)
标签:int 实现 nod others nyoj code 计数 return clu
原文地址:https://www.cnblogs.com/GetcharZp/p/8987259.html