您的位置:首页 > 其它

数组中寻最大递增序列2

2008-03-14 13:16 351 查看
#include <stdio.h>
#include <string.h>

#define MAX 256
int main(void)
{
char s[] = {8, 23, 6, 4, 6, 22, 4, 1, 89, 74};
char stk[MAX];
char l[MAX];
int m[MAX];
int n;
int i, j, top, max, pos;

n = sizeof(s)/sizeof(s[0]);

l[0] = 1;
m[0] = -1;
for (i=1;i<n;i++)
{
max = 0;
pos = -1;

for (j=0;j<i;j++)
if (s[j]<s[i]&&l[j]>max)
max = l[j], pos = j;

l[i] = max + 1;
m[i] = pos;
}

max = -1;
for (i=0;i<n;i++)
if (l[i]>max)
max=l[i],pos=i;

top = 0;
for (j=0;j<max;j++)
stk[top++] = pos, pos = m[pos];

printf("the max is: %d ",max);

while (top--) printf("%d ",stk[top]);
printf(" ");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐