Advertisement
Masovski

[Java Basics][Collections-HW] 04. Longest Increasing Seq

May 24th, 2014
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. import java.util.TreeMap;
  4.  
  5. public class LongestIncreasingSequence {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.        
  10.         TreeMap<String, String> longestMap = new TreeMap<>();
  11.         String longestStr = "";
  12.         String input = sc.nextLine();
  13.         String[] strArr = input.split(" ");
  14.         int[] intArr = new int[strArr.length];
  15.         String currentKey = null;
  16.         boolean isCounting = false;
  17.  
  18.  
  19.         for (int i = 0; i < strArr.length; i++) {
  20.             intArr[i] = Integer.parseInt(strArr[i]);
  21.         }
  22.        
  23.         for (int i = 0; i < intArr.length - 1; i++) {
  24.             if(currentKey == null) {
  25.                 currentKey = i + "";
  26.             }
  27.             if(intArr[i] < intArr[i + 1]) {
  28.                 if(!isCounting) {
  29.                     longestMap.put(i + "", "");
  30.                     currentKey = i + "";
  31.                     isCounting = true;
  32.                 }
  33.                 if(isCounting) {
  34.                     System.out.print(intArr[i] +  " ");
  35.                     longestMap.put(currentKey, longestMap.get(currentKey) + intArr[i] + " ");
  36.                     if(i == intArr.length - 2) {
  37.                         System.out.println(intArr[i + 1]);
  38.                         longestMap.put(currentKey, longestMap.get(currentKey) + intArr[i + 1] + " ");
  39.                     }
  40.                 }
  41.                
  42.             }
  43.             else {
  44.                 if(i >= 1 && intArr[i - 1] >= intArr[i]) {
  45.                     currentKey = i + "";
  46.                 }
  47.                 if(longestMap.get(currentKey) == null) {
  48.                     longestMap.put(currentKey, intArr[i] + " ");
  49.                 }
  50.                 else {
  51.                     longestMap.put(currentKey, longestMap.get(currentKey) + intArr[i] + " ");
  52.                 }
  53.                 System.out.println(intArr[i] + " ");
  54.                 if(i == intArr.length - 2) {
  55.                     System.out.println(intArr[i + 1]);
  56.                     longestMap.put(currentKey, intArr[i + 1] + " ");
  57.                 }
  58.                 isCounting = false;
  59.             }
  60.         }
  61.         for (String key : longestMap.keySet()) {
  62.             if(longestMap.get(key).length() / 2 > longestStr.length() / 2) {
  63.                 longestStr = longestMap.get(key);
  64.             }
  65.         }
  66.         System.out.println("Longest: " + longestStr);
  67.  
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement