给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。
输入格式:
输入第1行给出正整数n(≤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>
typedef struct node{
int data;
struct node *next;
}*Node;
Node init(){
Node head;
head=(Node)malloc(sizeof(struct node));
head->next=NULL;
return head;
}
Node insert(Node head){
Node pos,p;
pos=head;
int x;
scanf("%d",&x);
if(x==0)
{
printf("NULL");
return 0;
}
int i;
int pso=1;
while(x--){
pso++;
scanf("%d",&i);
p=(Node)malloc(sizeof(struct node));
p->data=i;
p->next=NULL;
pos->next=p;
pos=pos->next;
}
return head;
}
void sonList(Node L){
int h[100001]={0};
int z[100001]={0};
Node head,p;
int c=0 ,a=0,b=0,d=1,n=0,m=0,po=0;
p=L->next;
while(p->next){
n++;
z[a]=n;
if(p->data<p->next->data){
h[a]=++d;
p=p->next;
c=0;
po++;
continue;
}
if(c==0){
a++;
c++;
d=1;
}
p=p->next;
}
if(po==0){
printf("%d",L->next->data);
return 0;
}
z[a]=n+1;
int i=0,maxx=h[i],maindex=z[i];
for(i=0;i<=a;i++){
if(h[i]>maxx)
{
maxx=h[i];
maindex=z[i];
}
}
int fina;
fina=maindex-maxx;
p=L->next;
while(fina--){
p=p->next;
}
maxx--;
while(maxx--){
printf("%d ",p->data);
p=p->next;
}
printf("%d",p->data);
}
int main(){
Node head;
head=init();
head=insert(head);
if(head==NULL){
return 0;
}
sonList(head);
return 0;
}