Блог пользователя szawinis

Автор szawinis, история, 7 лет назад, По-английски

So, just a few hours ago, we finished a contest on a Thai site, namely Codecube. The round consisted of 6 problems to solve in 3 hours. I was feeling confident at first ACing two problems in 15 minutes. Then came the third problem, which was a typical two pointers + data structures. I was almost certain I could solve the problem, and started to code the solution. Many minutes passed by, and I finally got the solution. I submitted it, and got a WA slapped on my face. So, I simply moved on to the fourth problem and ACed it, because finding bugs is time consuming. I later returned to the third problem, and tried finding the bug for almost an hour with no luck, only to find out that the only line I messed up on was: freopen("data.txt", "r", stdin);, which I forgot to uncomment. I later submitted after the contest and got AC just with that line commented out. Absolutely disappointing.

As many of us know, the freopen command is used to redirect stdio to file IO. This is particularly useful when testing your program on a local machine because you can avoid copying and pasting the test data every time you want to debug. However, one of the problems I have experienced when using it is I forget to uncomment it out when submitting. This is a huge problem, especially on CF where many points are deducted for resubmission. I don't know if it's bad luck, but this has happened to me several times already. For anyone who is experiencing the same problem, I think the solution here is simply to write comments to remind yourself. Has this ever happened to any of you guys? Or is it just me being awfully stupid?

  • Проголосовать: нравится
  • -8
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +14 Проголосовать: не нравится

There is a way to avoid commenting and uncommenting that line. Simply use:

#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif

Personally, I don't use it, as my compiler allows me to redirect a file to stdin automatically. However, it should help you.

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится +24 Проголосовать: не нравится

I learn this from somewhere:

if (fopen("data.txt", "r")) {
   freopen("data.txt", "r", stdin);
}

It works in onsite contest as well.

»
7 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

./executable < input.txt

I worried about not commenting fread(), so I switched to this.
Type once, use command history to use as many times as you want.

»
7 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

My old ICPC teammate tiagob.reis used to do this all the time when we were training (redirect input and output and send the code to the judge without changing that), so almost all the problems he submitted had at least a +1 penalty — the verdicts varied from WA, TLE and RE (I can't remember, but in one judge we might have gotten some kind of forbidden operation error).

Fortunately, this never happened on any onsite contest for us.