Hello,
I have an error with my rust code https://codeforces.me/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 | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Hello,
I have an error with my rust code https://codeforces.me/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.me/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