// CSCI 3300
// Assignment: 7
// Author: Sam Twaiti
// File: lis.cpp
// Tab stops: 8
// this program takes a list of mountains and value from the standard input
// then prints out the longest increasing sublist from it.
#include <cstdio>
using namespace std;
#include <iostream>
#include <string>
const int maxNumbers = 100;
const int maxName = 100;
struct mountain
{
char* name;
int elevation;
mountain()
{
elevation = 0;
}
};
struct List
{
int item;
List* next;
List(List* nxt, int it)
{
next = nxt;
item = it;
}
};
struct bookOfMt
{
int numOfMts;
mountain* allMountains;
List* bis[maxNumbers];
bookOfMt(int nm, mountain* ma)
{
numOfMts = nm;
allMountains = ma;
}
};
bookOfMt newMountain()
{
mountain* n = new mountain[maxNumbers];
int count = 0;
int k;
for(k = 0; k < maxNumbers; k++)
{
n[k].name = new char[maxName];
scanf("%s", n[k].name);
scanf("%d", &n[k].elevation);
if(feof(stdin))
{
break;
}
if (n[k].elevation != 0)
{
count++;
}
}
bookOfMt z(count, n);
return z;
}
int positionS(bookOfMt z, int k, int count)
{
int s = count;
while(z.bis[s] != NULL && z.allMountains[z.bis[s]->item].elevation > z.allMountains[k].elevation)
{
s--;
}
return s+1;
}
int Counter(int s, int count)
{
return max(s, count);
}
List* reverseList(bookOfMt z, int count)
{
List* l = NULL;
while(z.bis[count] != NULL)
{
l = new List(l, z.bis[count]->item);
z.bis[count] = z.bis[count] -> next;
}
return l;
}
void resultPrinter(bookOfMt z, int count)
{
List* l = reverseList(z,count);
for(List* ptr = l; ptr != NULL; ptr = ptr -> next)
{
printf("%-30s ",z.allMountains[ptr->item].name);
printf("%10d\n",z.allMountains[ptr->item].elevation);
}
}
void insertCell(bookOfMt z)
{
int count = 0;
int k, s;
for(k = 0; k < z.numOfMts; k++)
{
s = positionS(z, k, count);
z.bis[s] = new List(z.bis[s-1],k);
count = Counter(s, count);
}
resultPrinter(z, count);
}
int main(int argc, char** argv)
{
bookOfMt z = newMountain();
int k;
for(k = 0; k < maxNumbers; k++)
{
z.bis[k] = NULL;
}
insertCell(z);
return 0;
}
Ly8gQ1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W50YWluc1twdHItPml0ZW1dLm5hbWUpOwoJCXByaW50ZigiJTEwZFxuIix6LmFsbE1vdW50YWluc1twdHItPml0ZW1dLmVsZXZhdGlvbik7Cgl9Cn0KCgp2b2lkIGluc2VydENlbGwoYm9va09mTXQgeikKewoJaW50IGNvdW50ID0gMDsKCWludCBrLCBzOwoJZm9yKGsgPSAwOyBrIDwgei5udW1PZk10czsgaysrKQkKCXsKCQlzID0gcG9zaXRpb25TKHosIGssIGNvdW50KTsKCQl6LmJpc1tzXSA9IG5ldyBMaXN0KHouYmlzW3MtMV0sayk7CgkJY291bnQgPSBDb3VudGVyKHMsIGNvdW50KTsKCX0KCXJlc3VsdFByaW50ZXIoeiwgY291bnQpOwp9CgoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIqKiBhcmd2KQp7Cglib29rT2ZNdCB6ID0gbmV3TW91bnRhaW4oKTsKCWludCBrOwoJZm9yKGsgPSAwOyBrIDwgbWF4TnVtYmVyczsgaysrKQoJewoJCXouYmlzW2tdID0gTlVMTDsKCX0KCWluc2VydENlbGwoeik7CglyZXR1cm4gMDsKfQoJCQo=
RWlnZXIgICAgICAgICAgICAzOTcwCkV2ZXJlc3QgICAgICAgICAgODg0OApEZW5hbGkgICAgICAgICAgIDU1MDAKRnVqaSAgICAgICAgICAgICAzNzc2CkdhbmdraGFyUHVlbnN1bSAgNzU3MApLMiAgICAgICAgICAgICAgIDg2MTEKS2lsYW1hbmphcm8gICAgICA1ODk1Ck1hdHRlcmhvcm4gICAgICAgNDQ3OApPbHltcHVzICAgICAgICAgIDI5MTcKVG9jb3B1cmkgICAgICAgICA1ODA4ClR1cHVuZ2F0byAgICAgICAgNjU3MApXaGl0bmV5ICAgICAgICAgIDQ0MjE=
Eiger 3970
Everest 8848
Denali 5500
Fuji 3776
GangkharPuensum 7570
K2 8611
Kilamanjaro 5895
Matterhorn 4478
Olympus 2917
Tocopuri 5808
Tupungato 6570
Whitney 4421