Hi!
I'm practicing coding in the new Rust language and today I'm struggling while solving this problem: 557E - Аня и полупалиндром. The main idea is to build a trie that contains all the suffixes of a binary string $$$s$$$ having a maximum length of $$$5000$$$.
I was able to code in C++ and get an AC, but when I switched to Rust, I got MLE verdict on test 9. Though both solutions have the same approach.
I think the main issue is that my $$$trie$$$ array contains too many elements and there are differences in the data storage mechanisms between C++ and Rust.
I'm new to this language anyway so any suggestions are appreciated. Thanks in advance!
int
in C++ takes 4 bytes, whileusize
in Rust takes 8 bytes. So if your C++ solution already uses around 400MB of memory, the Rust version withusize
shouldn't fit into 512MB limit.Easy (but ugly) fix — use
i32
oru32
instead ofusize
. One downside is that you can't index the array byi32
, so you will need to add some casts likeas usize
in some places. To make it more clean, you can store things inTrie
asu32
, but also add a wrapper function like:and just call
next(0)
instead of directly usingnext[0]
everywhere.