單調遞增最長子序列
時間限制:3000 ms | 內存限制:65535 KB
難度:4
-
描述
- 求一個字符串的最長遞增子序列的長度
如:dabdbf最長遞增子序列就是abdf,長度爲4
-
輸入
- 第一行一個整數0<n<20,表示有n個字符串要處理
隨後的n行,每行有一個字符串,該字符串的長度不會超過10000 -
輸出
- 輸出字符串的最長遞增子序列的長度
-
樣例輸入
-
3
aaa
ababc
abklmncdefg
-
樣例輸出
-
1
3
7
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#define Max_len 10001
void zuichang ();
int main(){
int n, i, j, len;
char ch[Max_len];
int length[Max_len], max;
scanf("%d", &n);
while (n --) {
memset(length, 0, Max_len);
scanf("%s", ch);
len = strlen(ch);
length[0] = 1;
for(i = 1; i < len; i++) {
length[i] = 1;
for(j = 0; j < i; j++) {
if(ch[i] > ch[j] && length[i] < length[j] + 1) {
length[i] = length[j] + 1;
}
}
}
max = length[0];
for(i = 1; i < len; i++) {
if(max < length[i])
max = length[i];
}
printf("%d\n", max);
}
}