Advertisement
SlavkovB

[АПС] Најдолга растечка подниза

Sep 18th, 2019
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. /**растечка подниза
  2. Да се најде најголемата растечка подниза во дадена листа
  3.  
  4. Влез:
  5. 13
  6. 1 2 3 1 2 3 4 1 2 3 4 5 2
  7.  
  8. Излез:
  9. 1 2 3 4 5
  10.  
  11. Влез:
  12. 13
  13. 1 2 3 1 2 3 4 1 2 3 4 5 6
  14. Излез:
  15. 1 2 3 4 5 6
  16. */
  17.  
  18.  
  19. //CODE
  20.  
  21.  
  22.  
  23.  
  24. import java.util.Scanner;
  25.  
  26. class SLLNode<E> {
  27.     protected E element;
  28.     protected SLLNode<E> succ;
  29.  
  30.     public SLLNode(E elem, SLLNode<E> succ) {
  31.         this.element = elem;
  32.         this.succ = succ;
  33.     }
  34.  
  35.     @Override
  36.     public String toString() {
  37.         return element.toString();
  38.     }
  39. }
  40.  
  41. class SLL<E> {
  42.     private SLLNode<E> first;
  43.  
  44.     public SLL() {
  45.         // Construct an empty SLL
  46.         this.first = null;
  47.     }
  48.  
  49.     public void deleteList() {
  50.         first = null;
  51.     }
  52.  
  53.     public int length() {
  54.         int ret;
  55.         if (first != null) {
  56.             SLLNode<E> tmp = first;
  57.             ret = 1;
  58.             while (tmp.succ != null) {
  59.                 tmp = tmp.succ;
  60.                 ret++;
  61.             }
  62.             return ret;
  63.         } else
  64.             return 0;
  65.  
  66.     }
  67.  
  68.     @Override
  69.     public String toString() {
  70.         String ret = new String();
  71.         if (first != null) {
  72.             SLLNode<E> tmp = first;
  73.             ret += tmp + "->";
  74.             while (tmp.succ != null) {
  75.                 tmp = tmp.succ;
  76.                 ret += tmp + "->";
  77.             }
  78.         } else
  79.             ret = "Prazna lista!!!";
  80.         return ret;
  81.     }
  82.  
  83.     public void insertFirst(E o) {
  84.         SLLNode<E> ins = new SLLNode<E>(o, first);
  85.         first = ins;
  86.     }
  87.  
  88.     public void insertAfter(E o, SLLNode<E> node) {
  89.         if (node != null) {
  90.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  91.             node.succ = ins;
  92.         } else {
  93.             System.out.println("Dadenot jazol e null");
  94.         }
  95.     }
  96.  
  97.     public void insertBefore(E o, SLLNode<E> before) {
  98.  
  99.         if (first != null) {
  100.             SLLNode<E> tmp = first;
  101.             if (first == before) {
  102.                 this.insertFirst(o);
  103.                 return;
  104.             }
  105.             // ako first!=before
  106.             while (tmp.succ != before)
  107.                 tmp = tmp.succ;
  108.             if (tmp.succ == before) {
  109.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  110.                 tmp.succ = ins;
  111.             } else {
  112.                 System.out.println("Elementot ne postoi vo listata");
  113.             }
  114.         } else {
  115.             System.out.println("Listata e prazna");
  116.         }
  117.     }
  118.  
  119.     public void insertLast(E o) {
  120.         if (first != null) {
  121.             SLLNode<E> tmp = first;
  122.             while (tmp.succ != null)
  123.                 tmp = tmp.succ;
  124.             SLLNode<E> ins = new SLLNode<E>(o, null);
  125.             tmp.succ = ins;
  126.         } else {
  127.             insertFirst(o);
  128.         }
  129.     }
  130.  
  131.     public E deleteFirst() {
  132.         if (first != null) {
  133.             SLLNode<E> tmp = first;
  134.             first = first.succ;
  135.             return tmp.element;
  136.         } else {
  137.             System.out.println("Listata e prazna");
  138.             return null;
  139.         }
  140.     }
  141.  
  142.     public E delete(SLLNode<E> node) {
  143.         if (first != null) {
  144.             SLLNode<E> tmp = first;
  145.             if (first == node) {
  146.                 return this.deleteFirst();
  147.             }
  148.             while (tmp.succ != node && tmp.succ.succ != null)
  149.                 tmp = tmp.succ;
  150.             if (tmp.succ == node) {
  151.                 tmp.succ = tmp.succ.succ;
  152.                 return node.element;
  153.             } else {
  154.                 System.out.println("Elementot ne postoi vo listata");
  155.                 return null;
  156.             }
  157.         } else {
  158.             System.out.println("Listata e prazna");
  159.             return null;
  160.         }
  161.  
  162.     }
  163.  
  164.     public SLLNode<E> getFirst() {
  165.         return first;
  166.     }
  167.  
  168.     public SLLNode<E> find(E o) {
  169.         if (first != null) {
  170.             SLLNode<E> tmp = first;
  171.             while (tmp.element != o && tmp.succ != null)
  172.                 tmp = tmp.succ;
  173.             if (tmp.element == o) {
  174.                 return tmp;
  175.             } else {
  176.                 System.out.println("Elementot ne postoi vo listata");
  177.             }
  178.         } else {
  179.             System.out.println("Listata e prazna");
  180.         }
  181.         return first;
  182.     }
  183. }
  184.  
  185. public class RasteckaPodniza {
  186.  
  187.     private static void RasteckaPodniza(SLL<Integer> list) {
  188.         SLLNode<Integer> current = list.getFirst();
  189.         SLLNode<Integer> next = current.succ;
  190.         int count = 0, max = 0;
  191.         int countPos = 0, prevEl = 0, position = 0;
  192.  
  193.         while (current != null) {
  194.             if (next == null) {
  195.                 if (prevEl < current.element) {
  196.                     ++count;
  197.                     if (count > max) {
  198.                         max = count;
  199.                         position = countPos + 1;
  200.                     }
  201.                 }
  202.                 break;
  203.             }
  204.             if (current.element < next.element) {
  205.                 ++count;
  206.             } else {
  207.                 if (count > max) {
  208.                     max = count;
  209.                     position = countPos;
  210.                     prevEl = current.element;
  211.                 }
  212.                 count = 0;
  213.             }
  214.             ++countPos;
  215.             next = next.succ;
  216.             current = current.succ;
  217.         }
  218.  
  219.         current = list.getFirst();
  220.         countPos = 0;
  221.         count = 0;
  222.         while (current != null) {
  223.             if (countPos >= (position - max) && countPos <= position && count < position - max - 1) {
  224.                 list.insertLast(current.element);
  225.                 ++count;
  226.             } else {
  227.                 list.delete(current);
  228.             }
  229.             ++countPos;
  230.             current = current.succ;
  231.         }
  232.  
  233.         System.out.println(list);
  234.     }
  235.  
  236.     public static void main(String[] args) {
  237.         Scanner input = new Scanner(System.in);
  238.         SLL<Integer> list = new SLL<Integer>();
  239.         int n = input.nextInt();
  240.  
  241.         for (int i = 0; i < n; i++)
  242.             list.insertLast(input.nextInt());
  243.         RasteckaPodniza(list);
  244.         input.close();
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement