最长递增字串

来源:互联网 发布:梦幻西游mac下载补丁 编辑:程序博客网 时间:2024/06/08 00:57
public class Zuichangdizengzichuang {    public static void main(String args[]) {        Integer[] a = new Integer[]{4,6,8,10,34,65,2,43,54,76,86,54,33,23,55};        MaxIncreSubSequence<Integer> mss = new MaxIncreSubSequence<Integer>(a);        System.out.println(mss.getNumOfMaxIncreSubSequence());        MaxIncreCloseSubSequence<Integer> mcss = new MaxIncreCloseSubSequence<Integer>(a);        System.out.println(mcss.getNumOfMaxIncreCloseSubSequence());    }    //连续最长递增子串    static class MaxIncreCloseSubSequence<T extends Comparable<? super T>>{        T[] t ;        public MaxIncreCloseSubSequence(T[] t) {            this.t = t;        }        public int getNumOfMaxIncreCloseSubSequence() {            int res = 0, find = 1;            for(int i = 0; i < t.length; i++) {                if(i + 1 < t.length && t[i].compareTo(t[i+1]) < 0) {                    find++;                } else {                    res = Math.max(find, res);                    find = 0;                }            }            return res;        }    }    //最长递增字串,不一定连续。用一个数组tmp保存计算过的数据,tmp[i]表示到i+1位置时的递增字串长度    static class MaxIncreSubSequence<T extends Comparable<? super T>>{        T[] t ;        public MaxIncreSubSequence(T[] t) {            this.t = t;        }        public int getNumOfMaxIncreSubSequence() {            int[] tmp = new int[t.length];            int res = 0;            for(int i = 0; i < t.length ; i++) {                tmp[i] = 1;                for(int j = 0; j < i; j++ ) {                    if(t[j].compareTo(t[i]) < 0)                         tmp[i] = Math.max(tmp[i], tmp[j] + 1);                }                res = Math.max(res, tmp[i]);            }            return res;        }    }}

结果是
9
6

0 0
原创粉丝点击