package com.iflytek.dp;
public class LongestNonDecrease {
private int[] s;
private int size;
private int[][] dp;
private LongestNonDecrease(int[] s){
this.s = s;
this.size = this.s.length;
this.dp = new int[this.size][];
for(int i = 0; i < this.size; i++){
this.dp[i] = new int[this.size];
}
}
public void fun() {
for(int i = 0; i < this.size; i++){
this.dp[i][i] = 1;
}
for (int i = 1; i < this.size; i++) {
for (int j = i - 1; j >= 0; j--) {
if (s[i] >= s[j]) {
//this.deepCopy(i, j);
this.deepCopy(this.dp[i], this.dp[j], j);
break;
}
}
}
}
public void deepCopy(int A[], int B[], int j){
for(int i = 0; i <= j; i++){
A[i] = B[i];
}
}
public void deepCopy(int i, int j){
for(int t = 0; t <= j; t++){
this.dp[i][t] = this.dp[j][t];
}
}
public static void main(String[] args) {
int arr[] = {13, 7, 9, 16, 38, 24, 37, 18};
LongestNonDecrease lnd = new LongestNonDecrease(arr);
lnd.fun();
System.out.println(lnd.dp);
}
}
public class LongestNonDecrease {
private int[] s;
private int size;
private int[][] dp;
private LongestNonDecrease(int[] s){
this.s = s;
this.size = this.s.length;
this.dp = new int[this.size][];
for(int i = 0; i < this.size; i++){
this.dp[i] = new int[this.size];
}
}
public void fun() {
for(int i = 0; i < this.size; i++){
this.dp[i][i] = 1;
}
for (int i = 1; i < this.size; i++) {
for (int j = i - 1; j >= 0; j--) {
if (s[i] >= s[j]) {
//this.deepCopy(i, j);
this.deepCopy(this.dp[i], this.dp[j], j);
break;
}
}
}
}
public void deepCopy(int A[], int B[], int j){
for(int i = 0; i <= j; i++){
A[i] = B[i];
}
}
public void deepCopy(int i, int j){
for(int t = 0; t <= j; t++){
this.dp[i][t] = this.dp[j][t];
}
}
public static void main(String[] args) {
int arr[] = {13, 7, 9, 16, 38, 24, 37, 18};
LongestNonDecrease lnd = new LongestNonDecrease(arr);
lnd.fun();
System.out.println(lnd.dp);
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29653519/viewspace-1713918/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29653519/viewspace-1713918/