Question: There are many trees with different names to calculate the probability of each tree appearing.
Analysis: String, Dictionary tree (trie ). Use the dictionary tree directly to count and then sort the output.
Note: No Number of test groups is available for poj2418. Only when the test group is detected by TLE a few times is complete.
# Include <algorithm> # include <iostream> # include <cstdlib> # include <cstring> # include <cstdio> using namespace STD; char words [32]; /* trie define */typedef struct node0 {char name [32]; int times;} tree; tree T [12001]; bool CMP (tree A, Tree B) {return strcmp (. name, B. name) <0 ;}# define nodesize 30003 // number of nodes # define dictsize 128 // character set size typedef struct node1 {int flag; // Value Field node1 * Next [dictsize];} tnode; TNO De dict [nodesize]; Class trie {PRIVATE: int size; int count; tnode * root; public: trie () {initial ();} void initial () {memset (dict, 0, sizeof (dict); Count = 0; size = 0; root = newnode ();} tnode * newnode () {return & dict [size ++];} void insert (char * Word) {tnode * Now = root; For (INT I = 0; word [I]; ++ I) {If (! Now-> next [word [I]) now-> next [word [I] = newnode (); now = now-> next [word [I];} if (! Now-> flag) {now-> flag = ++ count; strcpy (T [now-> flag-1]. name, word); t [now-> flag-1]. times = 1;} else t [now-> flag-1]. times ++;} void output (INT sum) {sort (t, t + count, CMP); For (INT I = 0; I <count; ++ I) printf ("% S %. 4lf \ n ", t [I]. name, 100.0/SUM * t [I]. times) ;}} trie;/* trie end */INT main () {int T, count; scanf ("% d", & T); getchar (); getchar (); While (t --) {trie. initial (); Count = 0; while (gets (words) & strlen (words) {trie. insert (words); count ++;} trie. output (count); If (t) printf ("\ n");} return 0 ;}
Ultraviolet A 10226-Hardwood Species