Zookeeper
1115: Shortest name Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 141 Solved: 56
[Submit] [Status] [Web Board] Description in a strange village, many people have long names, such as aaaaa, bbb and abababab. It is obviously inconvenient to call the full name because the name is so long. Therefore, it is generally called the prefix of a name between villagers. For example, you can call 'aaaaa' only, because the first three letters without a second name are 'aaa '. However, you cannot call it 'A' because two people have names starting with 'A. People in the village are very smart. They always use the shortest name. Input to ensure that no one in the village will have a prefix with the name of another (as an inference, no two will have the same name ). If a person in the village wants to name everyone (including himself), how many letters will he say? T (T <= 10 ). The first behavior of each group of data is an integer n (1 <=n <= 1000), that is, the number of people in the village. The name of each person in the following n rows (consisting of only lowercase letters ). Input to ensure that the length of the name of all persons in a village cannot exceed 1,000,000. Output
For each group of data, the total number of letters in the name of the person is output.
Sample Input
13aaaaabbbabababab
Sample Output
5
HINT
Source
Hunan eighth College Computer Program Design Competition
This topic is a dictionary tree question last time, and the idea is still unclear. I wrote a mistake last night. Today I drew my own picture of it, and the idea is clearer, the code can be easily written based on the idea. The idea is really important !!! Enhance your thinking skills!
The idea of the question is to store the name of each person in the dictionary tree. If the prefix is the same before the name, the node count increases by 1, and the number of tags of these nodes is added; the picture is ugly.
This can be used as your own dictionary tree template, and gradually accumulate your own template;
# Include
# Include
# Include
# Include
Using namespace std; typedef struct node // The struct {int count; // count struct node * next [26]; // The storage array (according to the meaning, can be changed)} node; // char s [1010] [10010]; // It is written like this by others, split a large array into two-dimensional arrays for writing (it seems that memory is a waste) char s [1000001]; node * newnode () // create a new node {node * q; q = (node *) malloc (sizeof (node); q-> count = 1; for (int I = 0; I <26; I ++) q-> next [I] = NULL; return q;} void build (node * T, char * s) // create a dictionary tree {node * p; p = T; int len, k; len = strlen (s); for (int I = 0; I
Next [k] = NULL) {p-> next [k] = newnode (); // p-> count = 1; // write this statement here, the answer has never been wrong. Later I wrote it to the function for creating a new node. It's right. p = p-> next [k];/* node * NewNode = (node *) malloc (sizeof (node); // here is another method. When used, the node NewNode-> count = 1; for (int j = 0; j <26; j ++) NewNode-> next [j] = NULL; p-> next [k] = NewNode; p = NewNode; */} else {p = p-> next [k]; p-> count ++; }}} int search (node * T) // query the dictionary tree {node * q; int sum = 0; q = T; for (int I = 0; I <26; I ++) {If (T-> next [I]! = NULL) {q = T-> next [I]; sum + = q-> count; if (q-> count> 1) {sum + = search (q) ;}} return sum;} void Release (node * T) // Release memory {for (int I = 0; I <26; I ++) if (T-> next [I]! = NULL) Release (T-> next [I]); // delete T; free (T);} int main () {int t, n; scanf ("% d", & t); while (t --) {node * T; T = (node *) malloc (sizeof (node )); t-> count = 0; for (int I = 0; I <26; I ++) T-> next [I] = NULL; scanf ("% d \ n", & n); for (int I = 0; I