Getting MLE,can anyone plz help.
problem:http://www.lightoj.com/volume_showproblem.php?problem=1114
Getting MLE,can anyone plz help.
problem:http://www.lightoj.com/volume_showproblem.php?problem=1114
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 154 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 146 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 135 |
| 9 | maroonrk | 133 |
| 10 | cry | 130 |
| Название |
|---|



Replace the pointer array with integer array.
Also you can replace integer array with map<char, int>. It probably will help to avoid MLE, but will increase time for accessing to the next trie vertex from O(1) to O(logN), where N is a number of vertex' children.
First, you have an unrelated bug: looks like you assume the alphabet has 25 letters instead of 26. Strangely, the solution gets AC anyway. Probably the tests are weak.
Your trie should take about (10^5 characters in dictionary)*(54 pointers in node)*(4 bytes in pointer)=21.6 MB with 32-bit compiler or about twice as much with 64-bit compiler. Since it gets MLE, the OJ probably uses 64-bit compiler. What can you do about it?
1) Instead of allocating the nodes individually with new, preallocate a sufficiently big array of nodes, and instead of 8-byte pointers keep 4-byte indices in this array. code. Replacing pointers with ints is usually not convenient. Though pre-allocating memory like this (and using pointers to it) is usually more convenient and more efficient than using new. Also note the neat one-line "deallocation" of the whole structure.
2) Scrap the trie and just use map<string,int> which, unlike the trie, doesn't take 52 times more memory than necessary. code.