Ex - Taboo Editorial by spheniscine


There is an alternative solution using a data structure called an Aho-Corasick trie. The implementation may even be more straightforward compared to the suffix array solution in the official editorial, though that may assume that you already have an implementation of Aho-Corasick (since that currently doesn’t exist in the AtCoder library)

To do better than \(O(N \sqrt {\sum |T_i|})\), you want (instead of climbing the dictionary-suffix links) to precalculate for each node of the trie: the length of the shortest dictionary word that’s a suffix of the string that node represents. This can be done by propagating that value down the suffix links; that way you can obtain that value in \(O(1)\) after each Aho-Corasick transition.

This should take in total of \(O(Nk)\) time and space, where \(k\) is the alphabet size.

posted:
last update: