fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Codechef
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. // your code goes here
  11. while(true) {
  12. String s =br.readLine();
  13. if(s.length()==1&&s.charAt(0)=='0') break;
  14. int num=codeShef(s);
  15. if(num==-1) System.out.println("Impossible");
  16. else System.out.println(num);
  17. }
  18. }
  19. public static int codeShef(String s) {
  20. char[] ary=s.toCharArray();
  21. boolean valid=true;
  22. int res=0;
  23. int i=0, j=ary.length-1;
  24. while(i<j) {
  25. //System.out.println(i);
  26. //System.out.println(j);
  27. if(ary[i]==ary[j]) {
  28. i++;
  29. j--;
  30. continue;
  31. }
  32. boolean found=false;
  33. int si=i+1;
  34. int ej=j-1;
  35. while(si<j&&ej>i) {
  36. //System.out.println(si);
  37. if(ary[si]==ary[j]) {
  38. found=true;
  39. while(si>i) {
  40. res++;
  41. ary[si]=ary[si-1];
  42. si--;
  43. }
  44. ary[i]=ary[j];
  45. break;
  46. }
  47. if(ary[ej]==ary[i]) {
  48. while(ej<j) {
  49. res++;
  50. ary[ej]=ary[ej+1];
  51. ej++;
  52. }
  53. ary[j]=ary[i];
  54. found=true;
  55. break;
  56. }
  57. si++;
  58. ej--;
  59. }
  60. //System.out.println("eeee");
  61. if(!found) return -1;
  62. i++;
  63. j--;
  64.  
  65. }
  66. //System.out.println(new String(ary));
  67. return res;
  68.  
  69. }
  70.  
  71. }
Runtime error #stdin #stdout #stderr 0.08s 32660KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NullPointerException
	at Codechef.main(Main.java:15)