Всем привет!
В ходе решения 4C - Система регистрации возникла интересная ситуация. Моё решение: 24184665. Суть в том, что мой компилятор и компилятор Codeforces выдает разные ответы. Компилятор — MS VS C++ 2010. Пример:
Входные данные: test data
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
Всем привет!
В ходе решения 4C - Система регистрации возникла интересная ситуация. Моё решение: 24184665. Суть в том, что мой компилятор и компилятор Codeforces выдает разные ответы. Компилятор — MS VS C++ 2010. Пример:
Входные данные: test data
Название |
---|
Since the standard doesn't specify the implementation of hash functions, the output is environment-dependent. Actually
std::hash
can even produce different hash values for the same input in different executions.(Since C++14)But during one execution it should return the same hash codes for the same values. It's the main principle of hash alghorithms. The question is in generation different hash-codes for the same objects during one execution.
The issue isn't "different hash-codes for the same objects during one execution" which is a library bug. It's the same hash value for different objects, in other words, hash collision. The string "idcvexvhgtyyvplfr" and "idcvexvhgtyyvplfrl" generates the same hash value for the compiler you selected.
MS VS C++ 2010 hash function only looks at a part of the string according to this StackOverflow post. A bit odd, but not a bug.
Yes. Maybe my previous comment isn't clear. I meant generating different hash for an object in an execution is a bug. Hash collision, although only looks part of a string is weird, isn't a bug.
Oh, I understood. Thank you, guys. The solution is very simple — write your own hash function.
Writing your own hash function is not going to prevent you from getting collisions. Is there a reason why you're not using a hash table?
There isn't. Simply, hash function was the first solution, which I found as optimal. But practice has shown that it isn't the best one. Thank you for a helpfull tips.