子序列與子串問題總結

來源:互聯網
上載者:User

標籤:子序列   子串   公用祖先   動態規劃   common prefix   

1、最長遞增子序列
擴充:雙端LIS
題目:從一列數中篩除儘可能少的數使得從左往右看,這些數是從小到大再從大到小的。該題是網易遊戲的筆試題,是一個典型的LIS變形,思路就是利用LIS計算兩個數組B,C。其中B[i]表示從左向右以i結尾的最長遞增子序列,C[i]表示從右向左n~i的最長遞增子序列,即i~n的最長遞減子序列,所以以data[i]作為最高點需要去掉的數字為n-B[i]-C[i]+1,具體代碼如下:
#include<iostream>#include<vector>using namespace std;int DoubleEndLIS(vector<int> data)//當然也可以使用O(nlogn)的演算法{int length = data.size(),i,j,res = length;if(length == 0)return 0;vector<int> B(length,1),C(length,1);for(i = 0;i < length;++i)//從左向右{for(j = 0;j < i;++j){if(data[j] < data[i] && B[j]+1 > B[i])B[i] = B[j]+1;}}for(i = length-1;i >= 0;--i)//從右向左{for(j = length-1;j > i;--j){if(data[j] < data[i] && C[j]+1 > C[i])C[i] = C[j]+1;}}for(i = 0;i < length;++i){if(length - B[i] - C[i] + 1 < res)res = length - B[i] - C[i] + 1;}return res;}int main(){int n,i;while(cin >> n){vector<int> data(n);for(i=0;i < n;++i)cin >> data[i];cout << DoubleEndLIS(data) << endl;}}

2、最長公用子序列
3、最長公用子串
4、最長公用首碼
5、最長公用祖先
6、最長重複子串(可重疊)
7、最長不重複子串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.