单调递增最长子序列
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
4
-
描述
-
求一个字符串的最长递增子序列的长度
如:dabdbf最长递增子序列就是abdf,长度为4-
输入
-
第一行一个整数0<n<20,表示有n个字符串要处理
随后的n行,每行有一个字符串,该字符串的长度不会超过10000
输出
- 输出字符串的最长递增子序列的长度 样例输入
-
3 aaa ababc abklmncdefg
样例输出
-
1 3 7
-
第一行一个整数0<n<20,表示有n个字符串要处理
低级算法:
#include <stdio.h>
#include <string.h>
#define maxn 10000 + 5
int max(int a, int b){
return a > b ? a : b;
}
char str[maxn];
int maxlen[maxn], len, ans; //maxlen[i]数组表示以第i个元素结尾的LCS值
void lcs(){
maxlen[0] = 1;
ans = 1;
for(int i = 1; i < len; ++i){
maxlen[i] = 1;
for(int j = 0; j < i; ++j){
if(str[i] > str[j]){
maxlen[i] = max(maxlen[i], maxlen[j] + 1);
}
}
ans = max(ans, maxlen[i]);
}
}
int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%s", str);
len = strlen(str);
lcs();
printf("%d\n", ans);
}
return 0;
}
优化后的二分法:
#include <stdio.h>
#include <string.h>
#define maxn 10000 + 5
char str[maxn], dp[maxn]; //dp[i]存储长度为i的最小字符,元素严格递增
int ok;
int binarySearch(char k, int l, int r){
int mid;
ok = 0;
while(l <= r){
mid = (l + r) / 2;
if(k > dp[mid]) l = mid + 1;
else if(k < dp[mid]) r = mid - 1;
else {ok = 1; break;}
}
return l;
}
int main(){
int t, len, k, count;
scanf("%d", &t);
while(t--){
scanf("%s", str);
len = strlen(str);
dp[1] = str[0]; //长度为1的字符是str[0]
count = 1;
for(int i = 1; i < len; ++i){
//二分法找到第一个不小于str[i]的元素
k = binarySearch(str[i], 1, count);
if(ok == 0){ //如果没找到,更新dp数组
dp[k] = str[i];
if(k > count) count = k;
}
}
printf("%d\n", count);
}
return 0;
}