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;
}
}
}
}
