kk_ducviet's blog

By kk_ducviet, 9 years ago, In English

Most coders read input from file and write out to another, but some online judge likes CodeForces use stdin. When solving problems in a contest, eveyone always want to minimum submission time, these little code may help:

C/C++

#ifndef ONLINE_JUDGE
freopen(fi, "r", stdin);
freopen(fo, "w", stdout);
#else
// online submission
#endif

Pascal

{$IFDEF ONLINE_JUDGE}
{online submission}
{$ELSE}
{read & write file}
{$ENDIF}   

Why? Because on the online judge, ONLINE_JUDGE is defined to be true in C/C++

Notice that it also works on UVa, some others too but I didn't test it :)

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

| Write comment?
»
9 years ago, # |
  Vote: I like it +4 Vote: I do not like it

It's better to define your own preprocessor option, for example LOCAL_PROJECT (and use it in all online judges).

You can do it using -DLOCAL_PROJECT for g++. And just check it with

#ifdef LOCAL_PROJECT
  freopen("a.in","r",stdin);
  //freopen("a.out","w",stdout); // console output is better (in most cases)
#else
  // add i/o method of specific testing system
#endif

MS VS users: Project -> Options -> C\C++ -> Preprocessor options.

And to use console output for debugging: Use Ctrl+F5 or _getch(); under #ifdef #endif in the end of your program. In this case, #include <conio.h> must me under #ifdef #endif too. Don't forget to run your project in Release configuration sometimes, it's much faster than Debug, but debugger won't work properly.

»
9 years ago, # |
  Vote: I like it +2 Vote: I do not like it

i usually read input from a file, but don't write output to a file (i use normal stdout).
during contests, all i do is change the command ./a.out to ./a.out < input.txt (works only on Linux-based OS, but similar addition can be made for other OS too).

  • »
    »
    9 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Don't lie to us, bash-like redirection also works in Windows, and, obviously, Mac, because it uses bash :)

»
9 years ago, # |
  Vote: I like it -10 Vote: I do not like it

It's pretty useless, because fast in/out only needed in 0.001% of cases.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I join contest with friends and once I submited it faster than all of them.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    There are a lot of problems where largest input is like 2 MB or even more.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It's not about how many ms this saves you. It eases your life alot when implementing. You don't have to manually input in the terminal. It gets you the input from the file, thus you see the answer in output file or stdout in no time!

»
8 years ago, # |
  Vote: I like it +4 Vote: I do not like it

in Java:

if (System.getProperty("ONLINE_JUDGE") == null) {
  // Input is a file
} else {
  // Input is System.in
}
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

is it available for python?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    import sys, os.path
    if(os.path.exists('input_file.txt')):
        sys.stdin = open("input_file.txt","r")
        sys.stdout = open("output_file.txt","w")
    
    
»
3 years ago, # |
  Vote: I like it -11 Vote: I do not like it

is this available for java?