Pagini recente » Cod sursa (job #1026948) | Cod sursa (job #1495455) | Cod sursa (job #738692) | Cod sursa (job #1788332) | Cod sursa (job #2346552)
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <queue>
typedef struct my_struct{
int length;
int how_you_got_there;
struct my_struct *next;
int index;
}MyArray;
using namespace std;
void free_list(MyArray **head)
{
MyArray *p=(*head);
MyArray *save;
while(p!=NULL)
{
save=p;
p=p->next;
free(save);
}
}
void recursion(MyArray *possible_lengths,int *sequence,int maximum_length,int index,FILE* write)
{
if(maximum_length==0)
return;
if(maximum_length!=1)
recursion(possible_lengths,sequence,maximum_length-1,possible_lengths[index].how_you_got_there,write);
else
recursion(possible_lengths,sequence,maximum_length-1,index,write);
fprintf(write,"%d ",sequence[index]);
}
int main()
{
ifstream read("scmax.in");
//ofstream write("scmax.out");
FILE *write=fopen("scmax.out","w");
int length_of_array;
read>>length_of_array;
int* sequence=(int*)calloc(length_of_array,sizeof(int));
MyArray *possible_lengths=(MyArray*)calloc(length_of_array,sizeof(MyArray));
MyArray *head;
for(int i=0;i<length_of_array;i++)
{
possible_lengths[i].how_you_got_there=-1;
read>>sequence[i];
}
possible_lengths[0].length=1;
MyArray *new_node=(MyArray*)calloc(1,sizeof(MyArray));
new_node->length=1;
new_node->index=0;
new_node->next=NULL;
head=new_node;
int maximum_length=1;
int index_of_maximum_length=-1;
MyArray *p;
MyArray *q;
for(int i=1;i<length_of_array;i++)
{
p=head;
while(p!=NULL && sequence[p->index]>=sequence[i])
p=p->next;
if(p!=NULL)
{
possible_lengths[i].length=p->length+1;
possible_lengths[i].how_you_got_there=p->index;
if(possible_lengths[i].length>maximum_length)
{
maximum_length=possible_lengths[i].length;
index_of_maximum_length=i;
}
new_node=(MyArray*)calloc(1,sizeof(MyArray));
new_node->index=i;
new_node->length=possible_lengths[i].length;
}
else if(p==NULL)
{
possible_lengths[i].length=1;
new_node=(MyArray*)calloc(1,sizeof(MyArray));
new_node->next=NULL;
new_node->index=i;
new_node->length=1;
}
p=head;
q=NULL;
while(p->length>possible_lengths[i].length && p!=NULL)
{
q=p;
p=p->next;
}
if(q==NULL)
{
new_node->next=head;
head=new_node;
}
else
{
q->next=new_node;
new_node->next=p;
}
}
fprintf(write,"%d\n",maximum_length);
recursion(possible_lengths,sequence,maximum_length,index_of_maximum_length,write);
//for(int i=0;i<length_of_array;i++)
// cout<<sequence[i]<<" "<<possible_lengths[i].length<<" "<<possible_lengths[i].how_you_got_there<<'\n';
free_list(&head);
free(sequence);
free(possible_lengths);
read.close();
fclose(write);
return 0;
}