I'm solving 1902E - Collapsing Strings in practice.
I have a solution with vectors and a solution with arrays. Here is the main difference between them, though you're free to check the original submissions 386818452 and 386818901 in case there is some other reason.
struct Node {
int sz;
int next[26];
};
// MLE version (>256M)
vector<Node> trie; trie.reserve(sumlength);
trie.pb(empty_node());
for (const string& s : strings) {
int cur = 0;
for (char c : s) {
if (trie[cur].next[c-'a'] == -1) {
trie[cur].next[c-'a'] = INT(trie.size());
trie.pb(empty_node());
}
cur = trie[cur].next[c-'a'];
trie[cur].sz++;
}
}
// AC version (~130M)
Node trie[sumlength+10];
trie[0] = empty_node();
int end_of_arr = 1;
for (const string& s : strings) {
int cur = 0;
for (char c : s) {
if (trie[cur].next[c-'a'] == -1) {
trie[cur].next[c-'a'] = end_of_arr;
trie[end_of_arr] = empty_node();
end_of_arr++;
}
cur = trie[cur].next[c-'a'];
trie[cur].sz++;
}
}
Basically I'm just storing trie nodes in a vector/array and there should be at most something like 1 million of them. I have an assert in the vector version that the total size of the vector does not exceed something like sumlength+100. The problem also guarantees that sumlength $$$\le 10^6$$$, and I have an assert to check that and it doesn't trip.
Does anyone know what's wrong? In theory, 1 million elements * struct of 108 bytes should be somwhere like 110 MB, and I believe vector overhead really happens in multidimensional vectors.








I think this might be the reason
We know for std::vector the no. of elements becomes twice (in GCC) and 1.5x in MSVC. so maybe when you reserved and then pushbacked. you pushbacked more than what you reserved which caused a vector reallocation. So it allocated another contiguous block of 2 * sumlength which is the reason for MLE. Try to reserve sumlength + 100.
That's probably it. If I just reserved sumlength+1. kms
I don't know what to comment, but I must comment on every blogs of greateric since I'm in greateric fan club...
you got me
i finally got evidence that greateric is a cheater!
really... Your evidence is soo strong, I'm starting to change my mind about greateric...