Title Description:
Autocompletepoints
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.
Example Input·Example Output·
55hihellololhillshill5aaaaaaaaaaaaaaa5aaaaaaaaaaaaaaa6tobeornottwobee3havingfunyet
Case #1:11Case #2:15Case #3:11Case #4:9Case #5:3
Problem Solving Ideas:
Use the dictionary tree to find out if the string has a common prefix with other strings. Specific look at the code
Title Code:
#include <set> #include <map> #include <queue> #include <math.h> #include <vector># Include <string> #include <stdio.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <cctype> #include <algorithm> #define EPS 1e-10#define pi acos ( -1.0) #define INF 107374182#define INF64 1152921504606846976#define LC l,m,tr<<1#define RC m + 1,r,tr<<1|1#define Zero (a) fabs (a) <eps#define iabs (x) ((x) > 0? (x):-(x)) #define CLEAR1 (A, X, SIZE) memset (A, X, sizeof (a[0]) * (min (size,sizeof (a))) #define ClearAll (A, X) memset (A, X , sizeof (a)) #define MEMCOPY1 (A, X, SIZE) memcpy (A, X, sizeof (X[0)) * (size)) #define Memcopyall (A, X) memcpy (A, X, sizeof ( x) #define MAX (x, Y) (((x) > (y))? (x): (y)) #define min (x, y) (((x) < (y))? (x): (y)) using namespace std;struct node{int p[27]; int cnt; BOOL Alive;} Treenode[1000005];int Ans,nodecnt;char s[1000005];void Insertto () {int Len=strlen (s), P=0; ans++; if (treenode[p].p[s[0]-' a ']==0) {clearall (treenode[nodecnt].p,0); treenode[nodecnt].cnt=0; Treenode[nodecnt].alive=false; treenode[p].p[s[0]-' a ']=nodecnt++; } p=treenode[p].p[s[0]-' a ']; treenode[p].cnt++; for (int i=1; i<len; i++) {if (treenode[p].p[s[i]-' a ']==0) {ClearAll (treenode[nodecnt].p,0) ; treenode[nodecnt].cnt=0; Treenode[nodecnt].alive=false; treenode[p].p[s[i]-' a ']=nodecnt++; } if (treenode[p].cnt==1&&treenode[treenode[p].p[s[i]-' a ']].cnt==0) {} else ans++; treenode[treenode[p].p[s[i]-' a ']].cnt++; p= treenode[p].p[s[i]-' a ']; }}int Main () {//freopen ("data.in", "R", stdin); Freopen ("Data.txt", "w", stdout); int t,case1=1; while (scanf ("%d", &t)!=eof) {while (t--) {ClearAll (treenode[0].p,0); treenode[0].cnt=0; Treenode[0].alive=false; Nodecnt=1; ans=0; int n; scanf ("%d", &n); for (int i=0; i<n; i++) {scanf ("%s", s); Insertto (); printf ("%d\n", ans); } printf ("Case #%d:%d\n", Case1++,ans); }} return 0;}
Topic Final Test data:
Links: http://pan.baidu.com/s/1o6oiwY2
Password: 1DGP
Facebook Hacker Cup Round 1 Autocomplete (test data included)