fork(2) download
  1. import java.util.ArrayList;
  2. import java.io.*;
  3.  
  4. class Lexicography {
  5.  
  6. static ArrayList<Long> perms = new ArrayList<Long>();
  7.  
  8. public static void main(String[] args) throws IOException{
  9. long time = System.currentTimeMillis();
  10. getPermutations(Integer.valueOf(new BufferedReader(new InputStreamReader(System.in)).readLine()));
  11.  
  12. System.out.println("\nSize:-"+perms.size()+"\nTime:-"+(System.currentTimeMillis()-time));
  13. //This println is never printed... java aborts before that
  14. }
  15.  
  16. public static ArrayList<Long> getPermutations(int num){
  17. int[] n = new int[num+1];
  18. for(int i=0; i<=num; i++) n[i]=i;
  19. perms.add(getLong(n));
  20. for(int i=num; i>=0; i--) permutate(n[i],n);
  21. return perms;
  22. }
  23.  
  24. private static void permutate(int k, int[] n){
  25. if(k>n.length) return;
  26. int p=0;
  27. for(int i:n){ if(i==k) break; p++;}
  28.  
  29. for(int i=0; i<n.length; i++){
  30. if(i==p || n[i]<k) continue;
  31. n=swap(p,i,n);
  32. perms.add(getLong(n));
  33. //System.out.println("i:"+(i+1)+" k:"+k+" n:"+getLong(n));//this prints all permutations till the last one and then poof!
  34.  
  35. for(int j=k-1; j>=0; j--) permutate(j,n);
  36. n=swap(p,i,n);
  37. }
  38. }
  39.  
  40. private static int[] swap(int i, int f, int[] a){
  41. int t=a[f];
  42. a[f]=a[i]; a[i]=t;
  43. return a;
  44. }
  45.  
  46. private static long getLong(int[] n){
  47. long ten=1, num=0;
  48. for(int i=n.length-1; i>=0; i--){
  49. num+=n[i]*ten; ten*=10;
  50. }
  51. return num;
  52. }
  53.  
  54. }
Runtime error #stdin #stdout 0.27s 245760KB
stdin
9
stdout
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0xb70cb2ed, pid=27876, tid=3031939952
#
# JRE version: 6.0_31-b04
# Java VM: Java HotSpot(TM) Client VM (20.6-b01 mixed mode, sharing linux-x86 )
# Problematic frame:
# V  [libjvm.so+0x3422ed]  MarkSweep::AdjustPointerClosure::do_oop(oopDesc**)+0xd
#
# An error report file with more information is saved as:
# /tmp/hs_err_pid27876.log
#
# If you would like to submit a bug report, please visit:
#   http://j...content-available-to-author-only...n.com/webapps/bugreport/crash.jsp
#