#include <cstdio>#include <iostream>#include <cstring>using namespace Std;#define LL Long LongConstintMaxnode =4000*1000+Ten;intHead[maxnode];int Next[Maxnode];intTot[maxnode];intVa[maxnode]; LL ans =0;intSZ =0;intInsert (char*s){intn = strlen (s);intU =0; for(inti =0; I <= n;i++) {intFlag =-1; for(intt = head[u];t;t =Next[T]) {if(Va[t] = =s[i]) {ans + = tot[t]; flag = t; } Ans+=tot[t]; }if(Flag = =-1){Next[SZ] = Head[u]; flag = SZ; Va[flag] =s[i]; TOT[SZ] =0; HEAD[SZ] =0; Head[u] = sz++; } u = Flag; tot[flag]++; }} Chars[1005];intMain () {intNintt =1;//Freopen ("1.txt","R", stdin);//Freopen ("2.txt","W", stdout); while(SCANF ("%d", &n)!=eof) {if(n = =0) Break; Ans =0; SZ =1; head[0] =0;Next[0] =0; tot[0] =0; for(inti =0; i < n;i++) {scanf ('%s',s); Inserts); }printf("Case %d: %lld\ n", T++,ans); } }
Very simple dictionary tree, did a day, with people to shoot a block of 2000 group, finally unexpectedly is LLD and i64d problem, do not say, spit blood go! But there are benefits, but also good, learned a new way of writing! It's OK!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
uva11732 Dictionary Tree