Exercises
The dictionary tree is built, and then DFS is ready.
Code:
#include <bits/stdc++.h>#defineCLR (x) memset (x,0,sizeof x)using namespacestd;Const intMaxnnode=10000010;Const intmaxn=100000;intsum;//the alphabet is the trie of all lowercase letters.structtrie{intch[maxn][ -]; intVal[maxnnode]; intSz//total number of nodes voidClear () {sz=1; CLR (ch[0]); CLR (val);}//initially there is only one root node intIdxCharc) {returnC-'a';}//the number of the character C voidInsert (Const Char*s) { intu=0, n=strlen (s); for(intI=0; i<n;i++){ intC=idx (s[i]); if(!ch[u][c])//Create a new node{CLR (Ch[sz]); VAL[SZ]=1;//nodes in the processch[u][c]=sz++;//number of the new node } Elseval[ch[u][c]]++; U=Ch[u][c]; } } intJudge (intUinttmp) { intsum=0; for(intI=0;i< -; i++){ if(!val[ch[u][i]])Continue; if(val[ch[u][i]]==1) sum+=tmp; ElseSum+=judge (ch[u][i],tmp+1); } returnsum; }}; Trie T;CharLine[maxnnode];intMain () {intT,n; scanf ("%d",&t); while(t--) {scanf ("%d",&N); T.clear (); for(intI=1; i<=n;i++) {scanf ("%s", line); T.insert (line); } Sum=0; printf ("%d\n", T.judge (0,1)); } return 0;}
CSU1115 the shortest name