-3

I must create hash table with operation speed O(1) for large text(search, paste, delete). Is it real? How minimize collision? Any example? I never used C++ later. i can't find any example hash table with dictionary for text.
Target language C++ (only STL).

Sergey
  • 37
  • 1
  • 2
  • 5
  • oh, really? https://www.google.pl/search?q=c%2B%2B+hash+table&oq=c%2B%2B+hash+table&aqs=chrome..69i57j0l5.4535j0j7&sourceid=chrome&es_sm=95&ie=UTF-8 – 4pie0 May 20 '14 at 14:18

1 Answers1

2

You can make use of unordered_map or unordered_set that are part of C++11 standard, or use boost's version of these containers if C++11 is not an option. If you need to implement the solution on your own I believe you can use the implementation of either as reference.

EDIT: as pointer out by amit this is still not constant as you need to hash a string, thus you need to traverse it at least once. Thus the complexity is O(|S|) where S is the string being searched. Also the complexity is expected O(|S|) as no matter how good the hash function collisions may occur(and except of the very rare case where a perfect hash can be used it will with very, very high probability due to the birthday paradox).

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Note that it is not O(1), rather O(|S|), where |S| is the string's length. Obviously, you cannot expect O(1), since you would need to read the whole string – amit May 20 '14 at 14:23
  • @amit that's right. Also it is amortized complexity. – Ivaylo Strandjev May 20 '14 at 14:24
  • It's not amortized, it's *expected*, which is a very different thing. – akappa May 20 '14 at 14:42
  • @NiklasB. amortized complexity is precisely about that. It does not deal with worst case. Just like push_back in a vector is amortized constant although worst case it doubles the size of the container. – Ivaylo Strandjev May 20 '14 at 14:52
  • @IvayloStrandjev Your definition of amortization seems to be off. Amortization means considering the cost of a sequence of operations and computing the resulting *mean cost* of a single operation. For example a series of n push_back/pop_back operations applied to an empty vector can be shown to be in O(n) *even in the worst case*, so the amortized cost of an operation is O(n)/n = O(1). Saying that "amortized complexity does not deal with worst case" is wrong – Niklas B. May 20 '14 at 14:54
  • @NiklasB. yes what you say is absolutely correct, I meant that amortized complexity does not deal with a single operation but rather with a sequence of operations, so your clarification is more accurate. – Ivaylo Strandjev May 20 '14 at 14:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54050/discussion-between-ivaylo-strandjev-and-niklas-b). – Ivaylo Strandjev May 20 '14 at 14:59