Uva11488: http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 24 & page = show_problem & problem = 2483
Question: give you n strings. For a prefix, if K appears, the length of the prefix * K will be obtained. Now, we ask you to calculate the maximum length * K.
Question: Use the trie tree. Insert each string into trie to record the number and length of each substring. The trie tree is easy to implement. After inserting, traverse each node, calculate the maximum Len * sum and output the result.
1 # include <cstring> 2 # include <vector> 3 # include <cstdio> 4 using namespace STD; 5 const int maxnode = 4000*1000 + 10; 6 const int sigma_size = 26; 7 char word [10002]; 8 int n, m; 9 int SZ; 10 struct trie {11 int head [maxnode]; // head [I] is the left son Id of node I 12 INT next [maxnode]; // next [I] is the right Brother Number of node I 13 char ch [maxnode]; // ch [I] is the character 14 int sum [maxnode] on node I; 15 int Len [maxnode]; 16 void clear () {17 SZ = 1; 1 8 head [0] = next [0] = 0; 19 memset (Len, 0, sizeof (LEN); 20 memset (sum, 0, sizeof (SUM )); 21} 22 void insert (const char * s, int form, int to) {23 int u = 0, V; 24 for (INT I = form; I <; I ++) {25 bool found = false; 26 for (V = head [u]; V! = 0; V = next [v]) 27 if (CH [v] = s [I]) {// 28 found = true; 29 break found; 30} 31 if (! Found) {32 V = SZ ++; // create a node 33 ch [v] = s [I]; 34 next [v] = head [u]; 35 head [u] = V; // Insert the first 36 head [v] = 0; 37} 38 U = V; 39 sum [u] ++; 40 Len [u] = I + 1; 41} 42} 43} trie; 44 int CAS; 45 int main () {46 scanf ("% d", & CAS ); 47 While (CAS --) {48 trie. clear (); 49 scanf ("% d", & N); 50 for (INT I = 1; I <= N; I ++) {51 scanf ("% s", word); 52 m = strlen (Word); 53 trie. insert (word, 0, m); 54} 55 int ans = 0; 56 for (INT I = 1; I <SZ; I ++) {57 ans = max (ANS, trie. len [I] * trie. sum [I]); 58} 59 printf ("% d \ n", ANS); 60} 61 Return 0; 62}
View code