NYOJ 17 單調遞增最長子序列(動態規劃),nyoj長子

來源:互聯網
上載者:User

NYOJ 17 單調遞增最長子序列(動態規劃),nyoj長子

時間限制:3000 ms  |  記憶體限制:65535 KB

難度:4

  • 描述

  • 求一個字串的最長遞增子序列的長度
    如:dabdbf最長遞增子序列就是abdf,長度為4

    • 輸入

    • 第一行一個整數0<n<20,表示有n個字串要處理
      隨後的n行,每行有一個字串,該字串的長度不會超過10000

    • 輸出

    • 輸出字串的最長遞增子序列的長度

    • 範例輸入

    • 3aaaababcabklmncdefg
    • 範例輸出

    • 13

      7


#include<iostream>#include<cstring> using namespace std; int dp[10000];char s[10000]; int SubSeq(char *s, int len){    int i,j,ans,max;    ans=0;    for(i=0;i<len;i++)    {        max=0;        for(j=i-1;j>=0;j--)        {            if(s[j]<s[i])                if(max<dp[j])                    max=dp[j];                         }        dp[i]=max+1;        if(ans<dp[i])            ans=dp[i];    }     return ans;} int main(){    int n,m;    cin >> n;;    while(n--)    {        cin>>s;        m=strlen(s);        memset(dp,0,sizeof(dp));        cout << SubSeq(s,m)<<endl;    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.