【算法】动态规划 最长上升子序列

来源:互联网 发布:淘宝上的淘是什么意思 编辑:程序博客网 时间:2024/04/30 02:47
/* * * 找出最长上升子序列的个数 * 并输出一个最长子序列 * */#include <stdio.h>#define MAX_STR_SIZE 50int main(){int i = 0;int j = 0;int Max = 0;int Maxlocate = 0;char str[MAX_STR_SIZE];int lque[MAX_STR_SIZE] = {0,};scanf("%s",str);while (str[i] != '\0'){lque[i] = 1;for (j = 0; j <= i; j++){if (str[i] > str[j]){lque[i] = lque[i] > lque[j]+1 ? lque[i] : lque[j]+1;if (Max < lque[i]){Max = lque[i];Maxlocate = i;}}}i++;}printf("\nThe Longest sequeue length is :%d\n",Max);Max--;char tmp = str[Maxlocate];printf("%c ",tmp);/* 输出时  从最大值开始 逆序输出 */while (--Maxlocate >= 0){if (Max >= 1 && Max == lque[Maxlocate] && tmp > str[Maxlocate]){Max--;tmp = str[Maxlocate];printf("%c ",tmp);}}printf("\n");return 0;}

0 0
原创粉丝点击