單調遞增最長子序列

來源:互聯網
上載者:User

標籤:演算法

單調遞增最長子序列

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

難度:4

描述

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

輸入

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

輸出

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

範例輸入

3aaaababcabklmncdefg

範例輸出

137

 

程式碼:
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

 

int leng1,leng2=26;

int num[27][10001];

char ch1[10001],ch2[]="abcdefghijklmnopqrstuvwxyz";

 

void LCSLength()

{

  int i,j;

  for(i=1;i<=leng2;i++)

    for(j=1;j<=leng1;j++)

    {

      if(ch1[j-1]==ch2[i-1])

        num[i][j]=num[i-1][j-1]+1;

      else

      {

        if(num[i-1][j]>num[i][j-1])

          num[i][j]=num[i-1][j];

        else 

          num[i][j]=num[i][j-1];

      }

    }

}

 

 

int main()

{

  int n;

  scanf("%d",&n);

  getchar();

  while(n--)

  {

    scanf("%s",ch1);

    leng1=strlen(ch1);

    LCSLength();

    printf("%d\n",num[leng2][leng1]);

  }

  system("pause");

  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.