khealer's blog

By khealer, history, 22 months ago, In English

Hello,

I have an error with my rust code https://mirror.codeforces.com/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

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
22 months ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

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://mirror.codeforces.com/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

  • »
    »
    22 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

    • »
      »
      »
      22 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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