Блог пользователя andrewtam

Автор andrewtam, история, 5 лет назад, По-английски

After the recent ABC, I realized I suck at string algorithms (the only one I know is hashing). What are the important string algos I should learn (for cp) and in what order should I learn them?

  • Проголосовать: нравится
  • +37
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +44 Проголосовать: не нравится

Em, String hashing should be enough for most problems, but if you want to go above and beyond, here are some cool things to learn.

Aho-Corasick.

Trie (Prefix Tree).

Suffix Array.

Suffix Tree.

KMP / Z-Algorithm / Manachers.

That should be about it.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +95 Проголосовать: не нравится

FYI you can get red without knowing any string algorithms other than hashing.

  • »
    »
    5 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    This is actually very interesting to me. I wonder why it is that many coders can get very far into their competitive programming journeys and not really use or learn string algorithms. I myself am an example of this, I have been doing cp for over a year now and have not really touched any string algorithms. Its not that string algorithms aren’t useful, I have seen them in plenty of problems and contests, but I just never bothered to learn them for some reason. From what I have observed of other coders, this is true for a decent amount of them as well.

  • »
    »
    5 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +12 Проголосовать: не нравится

    +1, I've still only solved 1 problem in contest that required a "string algorithm" (1466G - Song of the Sirens), and it was a while after I got red. [*]

    Still, it's nice to know a few so you don't end up completely stuck on a problem that requires them (and so you don't feel like there's a big gap in your knowledge).

    [*] but of course, I'm not sure if there were any problems I should have solved along the way, but lacked the strings knowledge.

  • »
    »
    5 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    Here's the strings problem that appeared in the latest ICPC Regionals from my country (Asia-Kanpur 2019).

    Would it be possible to solve this without any fancy strings algorithms?

»
5 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +25 Проголосовать: не нравится

Combine hashing with the idea that "if the total length of strings is limited with $$$L$$$, the number of different lengths among strings $$$\le \sqrt{2L}$$$" and you can be completely fine for quite a long time. However, at some point, you will understand that in some problems you basically do the same stuff as in, for example, suffix array, but with hashes. At this moment you will know that learning suffix structures is beneficial for you.

Before this magical moment, knowing how suffix structures work will not be as rewarding: even if you learn how they can be applied, solving problems (beyond educational ones) involving them during contests will be extra tough.

Still, learning simple stuff like prefix- or z- function is rewarding at any level.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

Eertree is a cool data structure that deals with palindromes (also called "Palindromic tree").

Usually, you won't need anything sophisticated like this (maybe for hard problems but still, it's rare), the most important string algorithms/data structures are the basic ones like hashing and trie, and maybe the more complicated (but still, not hard) Z-array and Manacher's algorithm.