單調遞增最長子序列

單調遞增最長子序列

時間限制: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);	
	}	
}        


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章