40: Redraiment的走法(不连续最长子字符串)

题目描述 :   Redraiment是走梅花桩的高手。Redraiment总是起点不限,从前到后,往高的桩子走,但走的步数最多,不知道为什么?你能替Redraiment研究他最多走的步数吗? 

样例输入

6

2 5 1 5 4 5

样例输出

3

提示

Example: 

6个点的高度各为 2 5 1 5 4 5 

如从第1格开始走,最多为3步, 2 4 5 

从第2格开始走,最多只有1步,5 

而从第3格开始走最多有3步,1 4 5 

从第5格开始走最多有2步,4 5

所以这个结果是3。

输入例子:

6

2

5

1

5

4

5

输出例子:

3

 

 1 //求不连续的最长递增子序列
 2 import java.util.ArrayList;
 3 import java.util.List;
 4 import java.util.Scanner;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9         Scanner in = new Scanner(System.in);
10         while(in.hasNextInt())
11         {
12             int num = in.nextInt();
13             int array[] = new int[num];
14             for(int i = 0; i<num; i++)
15             {
16                 array[i] = in.nextInt();
17             }
18             int max = GetResult(num, array);
19             System.out.println(max);
20         }
21     }
22      public static int GetResult( int num, int[] pInput)
23      {
24          //求不连续的最长递增子序列
25          int result[] = new int[num];//存放当前位置的最长字串长度
26          for(int i = 0;i<pInput.length;i++)
27          {
28              result[i] = 1;
29              for(int j = 0; j<i; j++)
30              {
31                  if(pInput[j] < pInput[i])
32                  {
33                      result[i] = Math.max(result[i], result[j]+1);
34                  }
35              }
36          }
37          int max = 0;
38          for(int i = 0;i<result.length;i++)
39          {
40              if(result[i] > max){max = result[i];}
41          }
42          return max;
43      }
44 }

 

posted @ 2016-08-30 09:44  sunshinelym  阅读(1598)  评论(0编辑  收藏  举报