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 | tourist | 3690 |
2 | jiangly | 3647 |
3 | Benq | 3581 |
4 | orzdevinwang | 3570 |
5 | Geothermal | 3569 |
5 | cnnfls_csy | 3569 |
7 | Radewoosh | 3509 |
8 | ecnerwala | 3486 |
9 | jqdai0815 | 3474 |
10 | gyh20 | 3447 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 164 |
3 | adamant | 163 |
4 | TheScrasse | 159 |
5 | nor | 157 |
6 | maroonrk | 155 |
7 | -is-this-fft- | 152 |
8 | Petr | 146 |
8 | orz | 146 |
10 | pajenegod | 145 |
Название |
---|
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.