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.



