Hello,
I have an error with my rust code https://codeforces.net/contest/231/submission/196189763.
I understand that I can't read the input but I don't understand the reason.
Can you help me? thanks
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Hello,
I have an error with my rust code https://codeforces.net/contest/231/submission/196189763.
I understand that I can't read the input but I don't understand the reason.
Can you help me? thanks
Название |
---|
Your input implementation has a few bugs in it that could be causing the runtime error.
First of all you're assuming that the only whitespace characters in the input are space and \n, but on windows there's also \r(among others), which would cause your code to not break out of the loop and try to parse the \r and thus panick/runtime error on windows.
I'm not sure if that's what's causing the error or if it is another bug in your input struct but the input implementation is definitely the issue.
There's also the fact that your input template is not very performant(you clear the buffer String several times and perform several system calls to read the next line instead of reading all at once).
Here's a shorter and better rust template for input which uses macros(Please note that this template does NOT work with interactive problem):
https://ideone.com/4Z2CE3
and here's your answer rewritten using this template, which gets an AC verdict:
https://codeforces.net/contest/231/submission/196308655
Hope this helps you out!
EDIT: For some reason codeforces freaks out from the rust code so I will place them in ideone instead
Thank you, I had understood my mistake. Your version is very fast.
When I clear the buffer, it is not just the index that is set to 0? that's what I thought.
When you .clear() a String what it's doing is that it goes through every character in that string and it has to drop all of that memory, therefore it's like you're iterating through the whole string again just to clear/drop it.
If you're interested this video basically perfectly explains how most of the data types and how the memory works in Rust: https://www.youtube.com/watch?v=rDoqT-a6UFg