E-The shortest nameTime
limit:5000MS
Memory Limit:65536KB
64bit IO Format:%lld &%llu Submit Status Practice CSU 1115
Description
in a strange village, many people's names are very long, such as AAAAA, BBB and Abababab. the name is so long that it is obviously inconvenient to call the full name. So the villagers usually only name the prefix. For example, "AAAAA" can be called only ' AAA ', because no second person's name of the first three letters is ' AAA '. But you can't call ' a ' because there are two people whose names start with ' a '. The people in the village are very clever, they always call people by the shortest call. Enter the guarantee that the village will not have a person whose name is the prefix of another person's name (as a corollary, the names of any two people will not be the same). if a person in the village wants to call everyone's name (including himself), how many letters will he say?
Input
Enter the number of first behavior Data group T (t<=10). The first behavior of each group of data is an integer n (1<=n<=1000), that is, the number of villagers. The following n lines each act on a person's name (consisting of only lowercase letters). Enter the sum of the lengths of the names of all the villagers in a village not exceeding 1,000,000.
Output
For each set of data, the total number of letters of the owner's name is output.
Sample Input
Sample Output
Analysis: A template problem for a dictionary tree
#include <iostream>#include<string.h>#include<stdio.h>using namespacestd;#defineMAX 28structnode{intFlag//whether the tag is a node intnum;;//indicates how many times a dictionary tree has the same number of prefixes structNode *Next[max];};voidInsert (Node *root,Char*str) { if(root==null&&*str==' /') return ; Node*p=Root; intLast//num that records the last node that was passed while(*str!=' /'){ if(p->next[*str-'a']==NULL) {Node*temp=Newnode; for(intI=0; i<max;i++) Temp->next[i]=NULL; Temp->flag=0; Temp->num=0; P->next[*str-'a']=temp; } P=p->next[*str-'a']; P->num+=1; STR++; } P->flag=1;}voidDelete (Node *root) { for(intI=0; i<max;i++) if(root->next[i]!=NULL) Delete (Root-Next[i]); Delete(root);}intQuery (Node *root,intCNT) { intsum=0; if(root->num==1) returnCNT; for(intI=0; i<max;i++) if(root->next[i]!=NULL) Sum+=query (root->next[i],cnt+1); returnsum;}intMain () {Charstr[1000005]; intT,n; CIN>>T; while(t--) {node*root=Newnode; for(intI=0; i<max;i++) Root->next[i]=NULL; Root->flag=0; CIN>>N; for(intI=0; i<n;i++) {cin>>str; Insert (ROOT,STR); } cout<<query (Root,0) <<Endl; Delete (root); } return 0;}
Hunan Province University student Program design contest original question E