Saturday, July 16, 2016

Java - Standard input and output in competitive programming

Java provides several classes for taking standard input and printing to standard output.


For input
  • Scanner class
  • BufferedReader and InputStreamReader classes
  • DataInputStream class
  • Console class
For output
  • PrintStream class (System.out.print)
  • BufferedWriter and OutputStreamWriter classes
  • PrintWriter class
We can use any of the above classes to interact with std in/out. But the performance of the above classes differ with the way they are implemented.

Sometimes the time taken to get user input or the time taken to print back to the user becomes important. One such occasion is competitive programming.

Competitive programming websites like hackerrank.com or codeforces.com sets timouts to run the solutions. These timeouts are generally set to check whether the competitor has solved the problem using an efficient algorithm.

But sometimes although the competitor has used an efficient algorithm, timeouts may occur. This is due to inefficiency in taking input or printing the output. Below is an implementation of a java class which can be easily used take input and print output efficiently. 

   
import java.io.*;
import java.text.*;
import java.math.*;
import java.util.*;
class IO
{
BufferedReader in;
OutputStream out;
public IO()
{
try
{
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedOutputStream (System.out);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/* Read an Integer */
public int iread()
{
try
{
return Integer.parseInt(readword());
}
catch(NumberFormatException e)
{
e.printStackTrace();
System.exit(1);
return -1;
}
}
/********************/
/* Read a double */
public double dread()
{
try
{
return Double.parseDouble(readword());
}
catch(NumberFormatException e)
{
e.printStackTrace();
System.exit(1);
return -1;
}
}
/*******************/
/* Read a long */
public long lread()
{
try
{
return Long.parseLong(readword());
}
catch(NumberFormatException e)
{
e.printStackTrace();
System.exit(1);
return -1;
}
}
/************************/
/* Read a single word */
public String readword()
{
StringBuilder b = new StringBuilder();
int c;
try
{
c = in.read();
while (c >= 0 && c <= ' ')
c = in.read();
if (c < 0)
return "";
while (c > ' ')
{
b.append((char) c);
c = in.read();
}
return b.toString();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
return "";
}
}
/************************/
/* Read an entire line */
public String readLine()
{
try
{
return in.readLine();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
return "";
}
}
/************************/
/* Read a BigInteger */
public BigInteger bread()
{
return new BigInteger(this.readword());
}
/*************************/
/* PRINTING */
/*************************/
/* Print an integer */
public void p(int obj)
{
try
{
out.write((obj+"").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/***************************/
/* Print an integer with newline */
public void pln(int obj)
{
try
{
out.write((obj+"\n").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/**************************/
/* Print a long */
public void p(long obj)
{
try
{
out.write((obj+"").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/***************************/
/* Print a long with newline */
public void pln(long obj)
{
try
{
out.write((obj+"\n").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/**************************/
/* Print a double */
public void p(double obj)
{
try
{
out.write((obj+"").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/***************************/
/* Print a double with newline */
public void pln(double obj)
{
try
{
out.write((obj+"\n").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/******************/
/* Print a string */
public void p(String obj)
{
try
{
out.write((obj).getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/*****************************/
/* Print a string with newline */
public void pln(String obj)
{
try
{
out.write((obj+"\n").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/***************************/
/* Print a BigInteger */
public void p(BigInteger obj)
{
try
{
out.write((obj+"").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/*****************************/
/* Print a BigInteger with newline */
public void pln(BigInteger obj)
{
try
{
out.write((obj+"\n").getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/**********************************/
/* Print a string array */
public void p(String[] obj)
{
try
{
for(String str: obj)
out.write((str + " ").getBytes());
out.write("\n".getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/*******************************/
/* Print an integer array */
public void p(int[] obj)
{
try
{
for(int el: obj)
out.write((el + " ").getBytes());
out.write("\n".getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/********************************/
/* Print a double array */
public void p(double[] obj)
{
try
{
for(double el: obj)
out.write((el + " ").getBytes());
out.write("\n".getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/********************************/
/* Print a long array */
public void p(long[] obj)
{
try
{
for(long el: obj)
out.write((el + " ").getBytes());
out.write("\n".getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/********************************/
/* Print a BigInteger array */
public void p(BigInteger[] obj)
{
try
{
for(BigInteger el: obj)
out.write((el+" ").getBytes());
out.write("\n".getBytes());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
view raw IO.java hosted with ❤ by GitHub

No comments:

Post a Comment

Optimized quick sort

In the previous article  we discussed about quick sort algorithm. In this article we are going to discuss about further optimizing quick sor...