Pagini recente » Cod sursa (job #2660674) | Cod sursa (job #2804379) | Cod sursa (job #3165203) | Cod sursa (job #3264184) | Cod sursa (job #2346410)
#include <iostream>
#include <fstream>
#include <stdlib.h>
typedef struct my_struct{
int length;
int how_you_got_there;
}MyArray;
using namespace std;
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));
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;
int maximum_length=1;
int index_of_maximum_length;
int partial_maximum_length;
int good_index;
int j;
for(int i=1;i<length_of_array;i++)
{
j=i-1;
partial_maximum_length=0;
good_index=-1;
while(j>=0)
{
if(sequence[j]<sequence[i] && partial_maximum_length<possible_lengths[j].length)
{
good_index=j;
partial_maximum_length=possible_lengths[j].length;
}
j--;
}
if(good_index!=-1)
{
possible_lengths[i].length=possible_lengths[good_index].length+1;
possible_lengths[i].how_you_got_there=good_index;
if(possible_lengths[i].length>maximum_length)
{
maximum_length=possible_lengths[i].length;
index_of_maximum_length=i;
}
}
else
possible_lengths[i].length=1;
}
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(sequence);
free(possible_lengths);
read.close();
fclose(write);
return 0;
}