Advertisement
porteno

Untitled

Apr 20th, 2020
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1.  
  2. public class Run {
  3.  
  4. public static void main(String[] args) {
  5. Node<Integer> chain1 = buildChain1();
  6. Node<Integer> chain2 = buildChain2();
  7.  
  8. printStack(tmpSol());
  9.  
  10. }
  11.  
  12. public static Stack<Integer> listsCompare(Node<Integer> pos1, Node<Integer> pos2) {
  13. Node<Integer> ps1 = pos1;
  14. Node<Integer> ps2 = pos2;
  15.  
  16. Stack<Integer> stk = new Stack<Integer>();
  17. int n = Math.max(listsize(pos1), listsize(pos1));
  18. while (n > 0) {
  19. if (ps1 == null || ps2 == null)
  20. return stk;
  21. if (ps1.getValue() != ps2.getValue())
  22. return stk;
  23.  
  24. stk.push(ps1.getValue());
  25. ps1 = ps1.getNext();
  26. ps2 = ps2.getNext();
  27.  
  28. n--;
  29. }
  30. return stk;
  31. }
  32.  
  33. public static Stack<Integer> tmpSol() {
  34. Node<Integer> lst1 = buildChain1();
  35. Node<Integer> lst2 = buildChain2();
  36.  
  37. Stack<Integer> max_set_stk = new Stack<Integer>();
  38. Node<Integer> pos1 = lst1;
  39. Node<Integer> pos2 = lst2;
  40. Node<Integer> tmp1 = pos1;
  41. Node<Integer> tmp2 = pos2;
  42. while (pos2 != null) {
  43. tmp2 = pos2;
  44.  
  45. while (pos1 != null) {
  46. boolean equality = false;
  47.  
  48. if (pos1.getValue() == pos2.getValue()) {
  49. equality = true;
  50. tmp1 = pos1;
  51. Stack<Integer> tmp_stk = listsCompare(pos1, pos2);
  52.  
  53. if (stackSize(max_set_stk) < stackSize(tmp_stk) ||
  54. max_set_stk.isEmpty()) {
  55. // empty previous stack values put newer
  56. while (!max_set_stk.isEmpty())
  57. max_set_stk.pop();
  58. while (!tmp_stk.isEmpty())
  59. max_set_stk.push(tmp_stk.pop());
  60. }
  61. }
  62. if(equality)
  63. pos1 = tmp1.getNext();
  64. else
  65. pos1 = pos1.getNext();
  66. pos2 = tmp2;
  67. }
  68. pos2 = pos2.getNext();
  69. pos1 = lst1;
  70.  
  71. } // end while
  72. // printStack(max_set_stk);
  73. return max_set_stk;
  74. }
  75.  
  76. // -------utils methods--------
  77. // prints the given chain
  78. public static void printChain(Node<Integer> first) {
  79. Node<Integer> tmp = first;
  80. while (tmp != null) {
  81. System.out.print(tmp.getValue() + "->");
  82. tmp = tmp.getNext();
  83. }
  84. System.out.print("->null");
  85.  
  86. }
  87.  
  88. // return size of given chain
  89. public static int listsize(Node<Integer> first) {
  90. Node<Integer> tmp = first;
  91. int size = 0;
  92. while (tmp.hasNext()) {
  93. size++;
  94. tmp = tmp.getNext();
  95. }
  96. return size + 1;
  97. }
  98.  
  99. public static int stackSize(Stack<Integer> s) {
  100. int count = 0;
  101. Stack<Integer> tmp = new Stack<Integer>();
  102. if (s.isEmpty())
  103. return count;
  104. while (!s.isEmpty()) {
  105. tmp.push(s.pop());
  106. count++;
  107. }
  108. while (!tmp.isEmpty()) {
  109. s.push(tmp.pop());
  110. }
  111. return count;
  112. }
  113.  
  114. public static void printStack(Stack<Integer> s) {
  115. Stack<Integer> tmp = new Stack<Integer>();
  116. while (!s.isEmpty()) {
  117. System.out.println("|_" + s.top() + "_|");
  118.  
  119. tmp.push(s.pop());
  120. }
  121. while (!tmp.isEmpty()) {
  122.  
  123. s.push(tmp.pop());
  124. }
  125. }
  126.  
  127. public static Node<Integer> buildChain2() {
  128. Node<Integer> nd1 = new Node<Integer>(6);
  129. Node<Integer> nd2 = new Node<Integer>(3, nd1);
  130. Node<Integer> nd3 = new Node<Integer>(6, nd2);
  131. Node<Integer> nd4 = new Node<Integer>(5, nd3);
  132. Node<Integer> nd5 = new Node<Integer>(9, nd4);
  133. Node<Integer> nd6 = new Node<Integer>(8, nd5);
  134. Node<Integer> nd7 = new Node<Integer>(4, nd6);
  135. Node<Integer> nd8 = new Node<Integer>(4, nd7);
  136. Node<Integer> nd9 = new Node<Integer>(5, nd8);
  137.  
  138. return nd9;
  139. }
  140.  
  141. public static Node<Integer> buildChain1() {
  142. Node<Integer> nd1 = new Node<Integer>(7);
  143. Node<Integer> nd2 = new Node<Integer>(6, nd1);
  144. Node<Integer> nd3 = new Node<Integer>(3, nd2);
  145. Node<Integer> nd4 = new Node<Integer>(4, nd3);
  146. Node<Integer> nd5 = new Node<Integer>(5, nd4);
  147. Node<Integer> nd6 = new Node<Integer>(9, nd5);
  148. Node<Integer> nd7 = new Node<Integer>(8, nd6);
  149. Node<Integer> nd8 = new Node<Integer>(4, nd7);
  150. Node<Integer> nd9 = new Node<Integer>(3, nd8);
  151. Node<Integer> nd10 = new Node<Integer>(6, nd9);
  152. Node<Integer> nd11 = new Node<Integer>(5, nd10);
  153. Node<Integer> nd12 = new Node<Integer>(1, nd11);
  154.  
  155. return nd12;
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement