fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String input = "(-1 +2 -3 -4 -5 -6 -7 +8 +9 +10 +11 +12 +13 +14 +15 -16 -17 -18 +19 -20 +21 +22 -23 -24 +25 -26 -27 +28 -29 -30 +31 +32 +33 -34 -35 +36 +37 +38 +39 -40 +41 +42 -43 +44 +45 +46 -47 +48 +49 +50 -51 +52 +53 +54 -55 -56 -57 +58 -59 -60 -61 +62 -63 +64 +65 -66 -67)";
  13. String indices = "116, 118, 73, 72";
  14. input = input.substring(1, input.length() - 1);
  15. String[] input1 = input.split(" ");
  16. int[] numbers = new int[input1.length];
  17. int n = input1.length;
  18. for (int i = 0; i < n; i++){
  19. numbers[i] = Integer.parseInt(input1[i]);
  20. }
  21. String[] indices1 = indices.split(", ");
  22. int[] indices2 = new int[4];
  23. for (int i = 0; i < 4; i++){
  24. indices2[i] = Integer.parseInt(indices1[i]) - 1;
  25. }
  26. int[] cycle = new int[n*2];
  27. for (int i = 0; i < n; i++) {
  28. if (numbers[i] > 0) {
  29. cycle[i*2] = (numbers[i] - 1)*2;
  30. cycle[i*2 + 1] = (numbers[i] - 1)*2 + 1;
  31. }
  32. else {
  33. cycle[i*2] = (numbers[i]*(-1) - 1)*2 + 1;
  34. cycle[i*2 + 1] = (numbers[i]*(-1) - 1)*2;
  35. }
  36. }
  37. int from = 0;
  38. int to = 0;
  39. n = n*2;
  40. for (int i = 0; i < n; i++) {
  41. if (cycle[i] == indices2[0]) {
  42. from = i;
  43. }
  44. if (cycle[i] == indices2[2]) {
  45. to = i;
  46. }
  47. }
  48. String cycle1 = "(";
  49. String cycle2 = "(";
  50. int i = from;
  51. while (i != (n + to - 1)%n) {
  52. if (cycle[(n + i)%n] < cycle[(n + i + 1)%n]) {
  53. cycle1 = cycle1 + "+" + (cycle[(n + i)%n]/2 + 1) + " ";
  54. }
  55. else {
  56. cycle1 = cycle1 + "-" + (cycle[(n + i)%n]/2 + 1) + " ";
  57. }
  58. i = (n + i + 2)%n;
  59. }
  60. cycle1 = cycle1.substring(0, cycle1.length() - 1) + ")";
  61. i = (n + to + 1)%n;
  62. while (i != (n + from)%n) {
  63. if (cycle[(n + i)%n] < cycle[(n + i + 1)%n]) {
  64. cycle2 = cycle2 + "+" + (cycle[(n + i)%n]/2 + 1) + " ";
  65. }
  66. else {
  67. cycle2 = cycle2 + "-" + (cycle[(n + i)%n]/2 + 1) + " ";
  68. }
  69. i = (n + i + 2)%n;
  70. }
  71. cycle2 = cycle2.substring(0, cycle2.length() - 1) + ")";
  72. System.out.print(cycle1);
  73. System.out.print(" ");
  74. System.out.print(cycle2);
  75. }
  76. }
Success #stdin #stdout 0.12s 36796KB
stdin
Standard input is empty
stdout
(+58 +59 +60 +61 +62 +63 +64 +65 +66 -67 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35) (+37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57)