Http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2081&pid=15
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - #pragmaComment (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1010Panax Notoginseng #defineMAXV 1010 - #defineMoD 1000000000 the using namespacestd; + Atypedefstructnode the { + intcount; - structNode *next[ -]; $}*Tree; $ - voidInsert (Tree h,Char*s) -{//Each node holds the number of occurrences of each letter. theTree p=h; - intlen=strlen (s);Wuyi for(intI=0; i<len;i++) the { - intindex=s[i]-'a'; Wu if(p->next[index]!=NULL) - { AboutP=p->Next[index]; $p->count++; - } - Else - { ATree tem= (tree)calloc(1,sizeof(node)); +Tem->count=1; thep->next[index]=tem; -p=tem; $ } the } the } the the intFind (Tree h) -{//Find a node that is not empty, plus the corresponding number of times, if the count value is greater than 1, then there is a branch, you need to continue to look down inTree p=h; the intsum=0, I; the for(intI=0;i< -; i++) About { the if(h->next[i]!=NULL) the { theP=h->Next[i]; +Sum+=p->count; - if(p->count>1) sum+=find (p); the }Bayi } the returnsum; the } - - Chars[ +]; the the intMain () the { the //freopen ("A.txt", "R", stdin); - intT,n; thescanf"%d",&t); the while(t--) the {94scanf"%d",&n); theTree head= (tree)calloc(1,sizeof(node)); the while(n--) the {98scanf"%s", s); About Insert (head,s); - }101printf"%d\n", find (head));102 Free(head);103 }104 return 0; the}
CSU-1115 Shortest name (Dictionary tree template problem)