给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。
输入格式:
输入第1行给出正整数n(\le 10^5≤105);;第2行给出n个整数,其间以空格分隔。
输入样例:
15
1 9 2 5 7 3 4 6 8 0 11 15 17 17 10
输出样例:
3 4 6 8
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1000001
///MAXSIZE大于1000000(题目要求的数值)是为了避免特殊情况溢出
typedef int ElementType;
struct LNode
{
ElementType Data[MAXSIZE];
int Length;
};
typedef struct LNode *List;
//初始化顺序表
List initSeqList()
{
List L;
L = (List)malloc(sizeof(struct LNode));
L->Length = 0;
return L;
}
//传值
void getValue(List L, int n)
{
List p = L;
int i;
for(i=0; i<n; i++)
scanf("%d", &(p->Data[i]));
p->Length = n;
}
//寻找最长连续递增子序列
void getMaxLength(List L, int n)
{
List p = L;
int i, j = 0, lengths = 1; //lengths为1, 表示长度只有头部
int lengthHead = 0; //lengthHead为该连续子列与下一个比较的前者(简称:比较头)
int lengthTail = 1; //lengthTail为该连续子列比较的后者(简称:比较尾)
int oldHead = 0; //每段连续子列的原头部下标
int maxLength; //最大长度的连续子列
int position; //最大长度的头点下标
for(i=0; i<n; i++)
{
if(p->Data[lengthHead] < p->Data[lengthTail]) //满足连续子列的条件
{
lengthHead++; //比较头 后移一位
lengthTail++; //比较尾 后移一位
lengths++; //该段长度+1
}
else if(p->Data[lengthHead] >= p->Data[lengthTail]) //不满足连续子列的条件
{
if(j == 0) //第一次传值
{
maxLength = lengths; //将该连续子列的长度赋值给length
position = oldHead; //将该连续子列的原头下标赋值给head
j++;
}
if(j == 1) //第二次及以上传值
{
if(maxLength < lengths)
{
maxLength = lengths;
position = oldHead;
}
}
lengthHead = lengthTail; //比较头变比较尾
oldHead = lengthTail; //原头变比较尾
lengthTail++; //比较尾向后移一位
lengths = 1; //长度重新赋值为1
}
}
for(i=0; i<maxLength; i++) //输出最长子列
{
if(i ==0 )
printf("%d", p->Data[position]);
else
printf(" %d", p->Data[position]);
position++;
}
}
int main()
{
List L;
int n;
L = initSeqList();
scanf("%d", &n);
getValue(L, n);
getMaxLength(L, n);
return 0;
}
完!