Since Crave State-of-the-art technology, you ' ve just purchased a phone with a great new feature:autocomplete! Your phone ' s version of AutoComplete has some pros and cons. On the one hand, it ' s very cautious. It is only autocompletes a word if it knows exactly what do you ' re trying to write. On the other hand, you have to teach it every word you want to use.
You have N distinct words, you ' d like to send in a text message in order. Before sending each word, you add it to your phone ' s dictionary. Then, you write the smallest non-empty prefix of the word necessary for your phone to autocomplete the word. This prefix must either be the whole word, or a prefix which are not a prefix of any other word yet in the dictionary.
What's the minimum number of letters you must type to send all N words?
Input
Input begins with an integer T, the number of test cases. For each test case, there was first a line containing the integer N. Then,N lines follow, each containing a word to send in the order you wish to send them.
Output
For the i-th test case, print a line containing ' case #I: ' followed by the minimum number of characters You need to type in your text message.
Constraints
1 ≤ T ≤100
1 ≤ N ≤100,000
The N words'll has a total length of no more than 1,000,000 characters.
The words is made up of only lower-case alphabetic characters.
The words is pairwise distinct.
Note: The input file is about 10-20MB.
Explanation of Sample
In the first test case, you'll write "H", "he", "L", "Hil", "Hill", for a total of 1 + 2 + 1 + 3 + 4 = one characters.
Give n words, add one at a time, find one, ask to find the word you added, at least you need to determine how many letters before you can determine the word.
Find the number of letters each word needs to determine.
#include <cstdio> #include <string> #include <cstring> #include <iostream>using namespace std; const int Maxn=1000100;int Dic[maxn][26],val[maxn],cnt;void Inst (string s) {int u,v,l,len; U=0,len=s.size (); for (l=0;l<len;l++) {v=s[l]-' a '; if (!dic[u][v]) {dic[u][v]=++cnt; memset (dic[cnt],0,sizeof dic[cnt]); } U=dic[u][v]; val[u]++; }}int get (string s) {int u,v,l,len,ret; U=0,len=s.size (); Ret=len; for (l=0;l<len-1;l++) {v=s[l]-' a ';; if (val[dic[u][v]]==1)//If this letter is 1, stating that there are no other words, to this can be determined {ret=l+1; Break } U=dic[u][v]; } return ret;} int main () {freopen ("Autocomplete.txt", "R", stdin); Freopen ("Outb.txt", "w", stdout); int t,cas=1,i,n; string S; scanf ("%d", &t); while (t--) {memset (dic[0],0,sizeof dic[0]); memset (val,0,sizeof val); cnt=0; scanf ("%d", &n); Int ans=0; for (i=0;i<n;i++) {cin>>s; Inst (s); Ans+=get (s); } printf ("Case #%d:%d\n", Cas++,ans); } return 0;}
Facebook Hacker Cup Round 1---Autocomplete