Animal statistics enhanced version time limit: 3000 MS | memory limit: 150000 KB difficulty: 4
-
Description
There are a large number of species in the virgin forest of the beautiful daxing'anling district. The list of unrecorded primitive animals is included in the animal data provided by the surveyors. Scientists want to determine which animal in the forest has the largest number, but because the data is too large, scientists can't stand it at last. They want to ask your ACMer for help.
Input
In the first line, enter the number of animal names N (1 <= N <= 4000000). In the next N lines, enter N strings to indicate the name of the animal (the length of the string cannot exceed 10, all strings are lowercase letters and only one group of test data is available ).
-
Output
Output the names and quantities of the most animals among them, and separate them with spaces (data ensures that there are no more than two types of animals ).
-
Sample Input
-
-
10
Boar
Pig
Sheep
Gazelle
Sheep
Sheep
Alpaca
Alpaca
Marmot
Mole
-
-
Sample output
-
-
Sheep 3
-
Code:
If the following questions are not modified in this way, an error will occur. When the animal name entered later is the prefix of the previous one, the number of occurrences of this name will be calculated,
So !!!
# Include "stdio. h "# include" stdlib. h "# include" string. h "# define null 0 typedef struct trie {int count; trie * child [26];} trie_node; trie_node * name; int count, n; char maxname [11]; void trie_insert (char c []) {int I, j; trie_node * p, * q; p = name; for (I = 0; I <strlen (c ); I ++) {if (p-> child [c [I]-'a'] = null) {q = (trie_node *) malloc (sizeof (trie_node )); p-> child [c [I]-'a'] = q; q-> count = 0; p = q; for (j = 0; j <26; j ++) q-> child [j] = NULL;} else {p = p-> child [c [I]-'a']; // q-> count ++; // p = q ;}// forp-> count ++; // I don't know why the above comments must be written like this, at lunch, I finally figured out if (p-> count) {count = p-> count; strcpy (maxname, c) ;}} int main () {int I; char str [11]; scanf ("% d", & n); count = 0; name = (trie_node *) malloc (sizeof (trie_node )); name-> count = 0; for (I = 0; I <26; I ++) name-> child [I] = null; while (n --) {scanf ("% s", str); trie_insert (str);} printf ("% s % d \ n", maxname, count); return 0 ;}