Why so mainstream? featuring data structures

Правка en1, от DanAlex, 2016-01-24 23:58:48

Seriously, why so serious? And why so mainstream? Competitive programming is all about fun, therefore...

Cutting to the chase

I'll talk about something that goes like that: everyone talks about it, few really know how to do it, everyone thinks everybody else knows how to do it, so everyone claims they know how to do it.Not teenage sex, but understanding data structures. In the sense of the metaphor before, I am close to a virgin but I still can give you some insight as there are some structures that are my type.

Don't get me wrong, I am not talking about mainstream stuff like segment trees, but not so usual data structure that tend to be implemented and modeled exactly the same way each time or even not implemented(cause STL has them). So, we're gonna talk about tries and heaps.

The good ol' dictionary

We'll try to implement a structure that can handle the following queries:

  • addw — add an apparition of word w in the data structure
  • deletew — delete an apparition of word w in the data structure
  • askw — get the number of apparition of word w in the list
  • lcpw — get a word from the data structure that has the longest common prefix with string w

Let's ignore the 4th type of query.

Now what's the simplest way to do this? Keep a list an loop through it. Nothing wrong with that, but we need faster stuff. The obvious thing that a programmer would think at is giving the words some order so that the search is faster. This is exactly what STL's unordered_map does. So a map would to the business. Supposing you want to implement it yourself, you should find a nice hashing function the problem might be solved. OK, I'll go home, we're done.

Give it a trie

At this moment we have a decent data structure but we did not solved the 4th query. Also, we did not use one information: we are working with strings. Now we need some observation to go on: if some strings have a common prefix, the prefix should be stored only once and we would need to somehow point to more places from that position.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en13 Английский DanAlex 2016-02-07 15:37:39 5 Tiny change: 's on it._ \n\n### Th' -> 's on it._ [cut]\n\n### Th'
en12 Английский DanAlex 2016-01-25 01:09:21 2 Tiny change: ' string:\n- $s$ > ' -> ' string:\n\n- $s$ > '
en11 Английский DanAlex 2016-01-25 00:49:36 0 Tiny change: 'u enjoyed!' -> 'u enjoyed!\n\n### ' (published)
en10 Английский DanAlex 2016-01-25 00:47:19 480
en9 Английский DanAlex 2016-01-25 00:42:53 482
en8 Английский DanAlex 2016-01-25 00:37:21 903
en7 Английский DanAlex 2016-01-25 00:27:54 966
en6 Английский DanAlex 2016-01-25 00:17:31 1867
en5 Английский DanAlex 2016-01-25 00:14:16 3 Tiny change: 'm $O(total_length * SI' -> 'm $O(totalLength * SI'
en4 Английский DanAlex 2016-01-25 00:13:47 25 Tiny change: ' now: \n\n[Your text to link here...](http://w' -> ' now: \n\n![ ](http://w'
en3 Английский DanAlex 2016-01-25 00:13:20 1449
en2 Английский DanAlex 2016-01-24 23:59:10 12
en1 Английский DanAlex 2016-01-24 23:58:48 2148 Initial revision (saved to drafts)