生活随笔
收集整理的這篇文章主要介紹了
求解最长单调递增子串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
求解最長遞增子串可分為兩種情況,即子串連續或非連續。
例如,對于整數串{1,3,5,1,-1,4,5,3,1,8,3,4,6,2,4,6,7,8,6,4}
其連續遞增子串為{2,4,6,7,8},非連續遞增子串為{{-1},{1},{2,4,6,7,8}}
連續遞增子串的求解思路:
采用動態規劃思想,令
lengthOfSubList[k]表示子串{list[0]...list[k]}中最長的連續遞增子串長度
lengthOfSubListIncludeK[k]表示子串{list[0]...list[k]}中以list[k]結尾的最長連續遞增子串長度
indexOfLastElement[k]表示子串{list[0]...list[k]}中最長的連續遞增子串的最后一個元素的位置
則
lengthOfSubListIncludeK[k] =
????????? lengthOfSubListIncludeK[k-1]+1,list[k]>=list[k-1]
????????? 1,????????????????????????????? list[k]<list[k-1]
lengthOfSubList[k] = max(lengthOfSubListIncludeK[k],lengthOfSubList[k-1])
indexOfLastElement[k] =
??????? k,????????????????????? lengthOfSubListIncludeK[k]>lengthOfSubList[k-1]
??????? indexOfLastElement[k-1],lengthOfSubListIncludeK[k]<=lengthOfSubList[k-1]
代碼:
#include?<stdlib.h>?#include?<stdio.h>??void?MaxIncrementSubList(int?list[],int?length)?{???????????????int*?lengthOfSubList?=?(int*)malloc(sizeof(int)*length);?????int*?lengthOfSubListIncludeK?=?(int*)malloc(sizeof(int)*length);?????int*?indexOfLastElement?=?(int*)malloc(sizeof(int)*length);?????int?i;??????????for?(i=0;i<length;i++)?????{?????????lengthOfSubList[i]?=?1;?????????lengthOfSubListIncludeK[i]?=?1;??????????indexOfLastElement[i]?=?i;???????}??????????for?(i=1;i<length;i++)?????{?????????if?(list[i]>=list[i-1])?lengthOfSubListIncludeK[i]?=?lengthOfSubListIncludeK[i-1]+1;?????????else?lengthOfSubListIncludeK[i]?=?1;?????????if?(lengthOfSubListIncludeK[i]>lengthOfSubList[i-1])?????????{?????????????lengthOfSubList[i]?=?lengthOfSubListIncludeK[i];?????????????indexOfLastElement[i]?=?i;?????????}?????????else?????????{?????????????lengthOfSubList[i]?=?lengthOfSubList[i-1];?????????????indexOfLastElement[i]?=?indexOfLastElement[i-1];?????????}?????}??????????printf("longest?sub?list?is:\n");?????int?idx?=?indexOfLastElement[length-1];?????while?(list[idx]>=list[idx-1])?????{?????????printf("[%d]=%d?",idx,list[idx]);?????????idx--;?????}?????printf("[%d]=%d?",idx,list[idx]);?}??int?main()?{?????int?list[20]?=?{1,3,5,1,-1,4,5,3,1,8,3,4,6,2,4,6,7,8,6,4};?????MaxIncrementSubList(list,20);?????int?a;?????scanf("%d",&a);?????return?0;?}? ?
非連續遞增子串的求解思路:
采用動態規劃思想,令
lengthOfSubList[k]表示子串{list[0]...list[k]}中最長的非連續遞增子串長度
lengthOfSubListIncludeK[k]表示子串{list[0]...list[k]}中以list[k]結尾的最長非連續遞增子串長度
indexOfLastElement[k]表示子串{list[0]...list[k]}中最長的非連續遞增子串的最后一個元素的位置
則
lengthOfSubListIncludeK[k] = max(lengthOfSubListIncludeK[i]+1, list[k]>=list[i], i=0..k-1)
lengthOfSubList[k] = max(lengthOfSubListIncludeK[k],lengthOfSubList[k-1])
indexOfLastElement[k] =
??????? k,????????????????????? lengthOfSubListIncludeK[k]>lengthOfSubList[k-1]
??????? indexOfLastElement[k-1],lengthOfSubListIncludeK[k]<=lengthOfSubList[k-1]
代碼:
#include?<stdlib.h>?#include?<stdio.h>??void?MaxIncrementSubList(int?list[],int?length)?{???????????????int*?lengthOfSubList?=?(int*)malloc(sizeof(int)*length);?????int*?lengthOfSubListIncludeK?=?(int*)malloc(sizeof(int)*length);?????int*?indexOfLastElement?=?(int*)malloc(sizeof(int)*length);?????int?i,j,max;??????????for?(i=0;i<length;i++)?????{?????????lengthOfSubList[i]?=?1;?????????lengthOfSubListIncludeK[i]?=?1;??????????indexOfLastElement[i]?=?i;???????}??????????for?(i=1;i<length;i++)?????{?????????max?=?lengthOfSubListIncludeK[i];?????????for?(j=0;j<i;j++)?????????{?????????????if?(list[i]>list[j])?????????????{?????????????????lengthOfSubListIncludeK[i]?=?lengthOfSubListIncludeK[j]+1;?????????????????if?(max<lengthOfSubListIncludeK[i])?max?=?lengthOfSubListIncludeK[i];?????????????}?????????}?????????lengthOfSubListIncludeK[i]?=?max;?????????if?(lengthOfSubListIncludeK[i]>lengthOfSubList[i-1])?????????{?????????????lengthOfSubList[i]?=?lengthOfSubListIncludeK[i];?????????????indexOfLastElement[i]?=?i;?????????}?????????else?????????{?????????????lengthOfSubList[i]?=?lengthOfSubList[i-1];?????????????indexOfLastElement[i]?=?indexOfLastElement[i-1];?????????}?????}??????????printf("longest?sub?list?is:\n");?????int?idx?=?indexOfLastElement[length-1];?????int?tmp?=?list[idx];?????for?(i=idx;i>=0;i--)?????{?????????if?(list[i]<=tmp)?????????{?????????????printf("[%d]=%d?",i,list[i]);?????????????tmp?=?list[i];?????????}?????}?}??int?main()?{?????int?list[20]?=?{1,3,5,1,-1,4,5,3,1,8,3,4,6,2,4,6,7,8,6,4};?????MaxIncrementSubList(list,20);?????int?a;?????scanf("%d",&a);?????return?0;?}? ?
?
轉載于:https://blog.51cto.com/zephiruswt/888847
總結
以上是生活随笔為你收集整理的求解最长单调递增子串的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。