您的位置:首页 > 理论基础 > 数据结构算法

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

2014-03-24 16:27 218 查看
/* *
* 找出最长上升子序列的个数
* 并输出一个最长子序列
* */
#include <stdio.h>
#define MAX_STR_SIZE 50

int 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息