Efficient Java IO..Please Help

Write here if you have problems with your Java source code

Moderator: Board moderators

Efficient Java IO..Please Help

Postby GUC.HassanMohamed » Wed Sep 10, 2008 2:38 am

Hello everybody,

I am having difficulties solving some problems using Java (TLE) but it passes after reimplementing it in C++
like (http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&page=show_problem&problem=1039) and I am pretty sure that this issue concerns the IO so I wonder if anybody could help me to enhance my IO efficiency.

Any help would be appreciated.

Here is my solution to the problem I mentioned using Java:
Code: Select all
import java.io.*;
import java.util.*;

class Main {
   public static void main(String[] args)throws IOException {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      int times = new Integer(br.readLine());
      String in;
      for(int i  = 0; i < times;i++){
         in = br.readLine();
         if(i != 0)
            System.out.println("");
         char [] ch = in.toCharArray();
         Arrays.sort(ch);
         System.out.write((new String(ch)+"\n").getBytes());
         while(j_next_permutation(ch))
            System.out.write((new String(ch)+"\n").getBytes());
      }
   }
   public static boolean j_next_permutation(char [] a) {
      int F = 0, L = a.length, I = a.length;
      if (F == L || F == --I) return false;
      for (;;) {
         int Ip = I;
         if (a[--I] < a[Ip]) {
            int J = L;
            for (; !(a[I] < a[--J]););
            char tmp = a[I]; a[I] = a[J]; a[J] = tmp;
            for (int i = Ip; i < ((L - Ip) % 2 == 0 ? Ip + (L - Ip) / 2 : Ip + (L - Ip) / 2 + 1); ++i) {
               char tm = a[i]; a[i] = a[a.length - 1 - i + Ip]; a[a.length - 1 - i + Ip] = tm;
            }
            return true;
         }
         if (I == F) {
            for (int i = 0; i < ((L - F) % 2 == 0 ? (L - F) / 2 : (L - F) / 2 + 1); ++i) {
               int tm = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = a[i];
            }
            return false;
         }
      }
   }
}
GUC.HassanMohamed
New poster
 
Posts: 3
Joined: Fri Aug 22, 2008 5:22 pm

Re: Efficient Java IO..Please Help

Postby lnr » Wed Oct 08, 2008 6:43 am

To GUC.HassanMohamed

Taking input seems alright to me.
May be taking input in java is slower than C or C++.
User avatar
lnr
Experienced poster
 
Posts: 134
Joined: Sat Jun 30, 2007 2:52 pm
Location: (DU,CSE)Dhaka,Bangladesh


Return to Java

Who is online

Users browsing this forum: No registered users and 0 guests