Input
N 2<=n<=4000
S1
S2
...
sn
1<=len (SI) <=1000
Output
Output with strcmp () 22 compare SI,SJ (i!=j) to compare the number of times, the result in a long long range (the same character compared two times, not the same character comparison, including ' ")
Practice: Because the character set is too large, to use the left brother's right son's trie to save characters, do not have to open ch[62 every time a child
1#include <cstdio>2#include <queue>3#include <cstring>4#include <iostream>5#include <cstdlib>6#include <algorithm>7#include <vector>8#include <map>9#include <Set>Ten#include <ctime> One#include <cmath> A#include <cctype> - #defineMAX 100000 - #defineLL Long Long the #defineMoD 20071027 - structnode - { - intsz; + CharVal; -node*ch[2];//Ch[1] brother, Ch[0] son + node () A { atch[0]=ch[1]=NULL; -sz=0; - } - }; - Charword[1010]; - intn,cas=1; in Long Longsum; - Long LongInsertChar*s,node*u) to { + Long Longsum=0, lastsz=u->sz++; - for(; *s| | * (S-1); s++) the { * if(!u->ch[0])//easy to mistake, you must first connect the new node to the parent node to go down, otherwise the parent can no longer read to that node. $ {Panax Notoginsengu->ch[0]=Newnode; -u->ch[0]->val=*s; the } + for(u=u->ch[0];u->val!=*s;u=u->ch[1]) A { the if(!u->ch[1]) + { -u->ch[1]=Newnode; $u->ch[1]->val=*s; $ } - } -Sum+=lastsz+u->sz; thelastsz=u->sz++; - }Wuyi returnsum; the } - voidFreenode (node*u) Wu { - if(U==null)return; AboutFreenode (u->ch[0]); $Freenode (u->ch[1]); - Deleteu; - } - intMain () A { + //freopen ("/home/user/desktop/in", "R", stdin); the while(SCANF ("%d", &n) = =1&&N) - { $sum=0; theNode *root=Newnode; the while(n--) the { thescanf"%s", word); -sum+=Insert (word,root); in } theprintf"Case %d:%lld\n", cas++, sum); the freenode (root); About } the //printf ("Time=%.3lf", (double) clock ()/clocks_per_sec); the return 0; the}
my Code
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd; 6 7 #defineREPF (I,A,B) for (int i= (a); i<= (b); i++)8typedefLong Longll; 9 Ten Const intN =0; One Const intMaxnode =4000010; A - intN, CAS; - ll ans; the Charstr[4001]; - - structStrie { - intSon[maxnode]; + intBro[maxnode]; - intVal[maxnode]; + CharCh[maxnode]; A intsz; at -Strie () {sz =1; ch[0] = val[0] = bro[0] = son[0] =0; } - voidInit () {sz =1; ch[0] = val[0] = bro[0] = son[0] =0; } - //Inline int idx (char c) {return C-' a ';} - - voidInsertChar*s) { in intLen = strlen (s), u =0, p; -REPF (I,0, Len) { to //Check the brother of U + for(p = son[u]; p; p =Bro[p]) { - if(Ch[p] = =S[i]) the Break; * } $ //cannot find out than insertPanax Notoginseng if(!p) { -p = sz++; theCH[P] =S[i]; +BRO[P] =Son[u]; ASON[P] =0; theVAL[P] =0; +Son[u] =p; - } $Ans + = (Val[u]-val[p]) * (2* i +1); $ if(len = =i) { -Ans + = val[p] * (2* i +2); -val[p]++; the } -val[u]++; WuyiU =p; the } - } Wu } trie; - About intMain () { $ //ios_base::sync_with_stdio (0); - while(~SCANF ("%d", &n) &&N) { - Trie.init (); -Ans =0; AREPF (I,0N1) { +scanf"%s", str); the Trie.insert (str); - } $printf"Case %d:%lld\n", ++cas, ans); the } the return 0; the}
Copy Code
UVA-11732 "strcmp ()" Anyone left brother right son Trie