Всем привет!
В ходе решения 4C - Система регистрации возникла интересная ситуация. Моё решение: 24184665. Суть в том, что мой компилятор и компилятор Codeforces выдает разные ответы. Компилятор — MS VS C++ 2010. Пример:
Входные данные: test data
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 170 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
5 | -is-this-fft- | 158 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
9 | awoo | 152 |
9 | luogu_official | 152 |
Всем привет!
В ходе решения 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.