单调递增最长子序列

 1 import java.util.*;
 2 import java.math.*;
 3 
 4 public class Main {
 5 
 6     public static void main(String[] args) {
 7         Solution s = new Solution();
 8         Scanner sc = new Scanner(System.in);
 9         int N = sc.nextInt();
10         int[] answer = new int[N];
11         for(int i = 0; i < N; i++) {
12             answer[i] = s.getResult(sc.next());
13         }
14         sc.close();
15         for(int i : answer) {
16             System.out.println(i);
17         }
18     }
19 }
20 class Solution {
21     public int getResult(String s) {
22         int N = s.length();
23         char[] chArray = s.toCharArray();
24         int[] states = new int[N];
25         
26         for(int i = 0; i < N; i++) {
27             int pickMax = 1;
28             for(int c = 0; c < i; c++) {
29                 if(chArray[c] < chArray[i]) {
30                     pickMax = states[c] + 1 > pickMax ? states[c] + 1 : pickMax;
31                 }
32             }
33             states[i] = pickMax;
34         }
35         int max = 1;
36         for(int i : states) {
37             if(i > max)
38                 max = i;
39         }
40         return max;
41     }
42 }

 题目不难,算是动态规划的入门吧

posted @ 2016-02-29 10:05  DavieTiming  阅读(244)  评论(0编辑  收藏  举报