NYOJ17,單調遞增最長子序列

來源:互聯網
上載者:User

標籤:style   http   color   java   os   io   for   ar   

單調遞增最長子序列時間限制:3000 ms  |  記憶體限制:65535 KB難度:4
描述
求一個字串的最長遞增子序列的長度
如:dabdbf最長遞增子序列就是abdf,長度為4
輸入
第一行一個整數0<n<20,表示有n個字串要處理
隨後的n行,每行有一個字串,該字串的長度不會超過10000
輸出
輸出字串的最長遞增子序列的長度
範例輸入
3aaaababcabklmncdefg
範例輸出
137
來源
經典題目


也是到經典的DP題,另一種思路是,就a—z的一個字串和該字串的最長公用子序列,想想也是對的


import java.util.Arrays;import java.util.Scanner;public class NYOJ17 {public static void main(String[] args) {Scanner cin=new Scanner(System.in);int t=cin.nextInt();while (t-->0){char []s=cin.next().toCharArray();int []f=new int[s.length+5];Arrays.fill(f, 1);//初始化為1,剛開始寫成0出錯了for (int i=2;i<=s.length;i++){//int max=Integer.MIN_VALUE;for (int j=1;j<=i-1;j++){if (s[i-1]>s[j-1] && f[i]<f[j]+1){f[i]=f[j]+1;}}}int max=Integer.MIN_VALUE;for (int i=1;i<=s.length;i++){if (max<f[i]) max=f[i];}System.out.println(max);}}}









聯繫我們

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