http://acm.hust.edu.cn/vjudge/problem/28438
Test instructions: Given n strings, ask the strcmp function to compare how many times these strings are compared.
Exercises
Insert a ' # ' as a string of terminator, avoiding the special judgment too messy.
When inserting, if you pass through the previously inserted characters, add the number of comparisons.
To use a long long.
1 //uva117322#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <iostream>6 using namespacestd;7 8typedefLong LongLL;9 ConstLL n=4100, l=1100;Ten CharS[l]; One LL N,num,ans; A structnode{ - LL D,son,right,sum,len; -}a[n*L]; the - voidClear (LL x) - { -a[x].d=a[x].sum=a[x].len=0; +a[x].son=a[x].right=-1; - } + A voidInsertChar*s,ll L) at { -LL x=0; - for(LL i=0; i<l;i++) - { - BOOLFound=0; -LL now= -, Next; in if(s[i]!='#') now=s[i]-'a'+1; - for(LL y=a[x].son;y!=-1; y=a[y].right) to { + if(a[y].d==Now ) - { theFound=1; *ans+=2*a[y].sum; $next=y;Panax Notoginseng } - Elseans+=a[y].sum; the } + if(!found) A { thenum++; + Clear (num); -A[num].d=Now ; $a[num].len=i+1; $ if(a[x].son==-1) a[x].son=num; - Else - { theLL t=A[x].son; - while(a[t].right!=-1) Wuyit=A[t].right; thea[t].right=num; - } Wu -next=num; About } $x=Next; -a[x].sum++; - } - } A + intMain () the { -Freopen ("a.in","R", stdin); $Freopen ("a.out","W", stdout); theLL t=0; the while(1) the { thescanf"%lld",&n); GetChar (); - if(!n)return 0; innum=0; ans=0; Clear (0); the for(LL i=1; i<=n;i++) the { Aboutscanf"%s", s); theLL l=strlen (s); thes[l]='#'; l++; the Insert (s,l); + } -printf"Case %lld:%lld\n",++T,ans); the }Bayi the return 0; the } - /* - a!=b 1 the a==b 2 the */
"uva11732-" strcmp () "Anyone?" Trie