数组中寻最大递增序列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;
}
#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;
}
相关文章推荐
- 最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和
- 最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和
- 最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和
- 求整形数组中最大递增序列的长度
- 最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和
- ] 找工作知识储备(2)---数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 最大子数组和、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离
- 【各种最...】最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和
- DP-最大递增子序列与最大递增子数组; 最大公共子序列与最大公共子数组。
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 寻找数组中的最大递增序列
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 找工作知识储备(2)---数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 数组中的最大递增子序列(Longest Increasing Subsequence<LIS>)
- 最大子数组之和、最大子数组之积、最长递增子序列求法
- 最大子数组之和、最大子数组之积、最长递增子序列求法
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串 (转)