/* * Andrew Donley * Dynamic Programming Problem: * Longest NonDecreasing Subsequence * June 18th, 2013 */#include "LongestNonDecreasingSubsequence.h"#include<iostream>#include<string>#include<sstream>usingnamespacestd;intmain(){getInput();computeMaxSub();displayResults();return0;}Position::Position(inttemp_number){number=temp_number;max_sub_sequence=1;}voidPosition::display(){cout<<endl<<"Number: "<<number<<", Index: "<<index<<", Max: "<<max_sub_sequence;return;}/* * Gets the sequence from user input */voidgetInput(){stringinput;intval,index=0;Position*temp;cout<<endl;cout<<"Longest Non-Decreasing Subsequence"<<endl;cout<<"=================================="<<endl;cout<<"Please enter a sequence of numbers deliniated by spaces."<<endl;cout<<"Press 'return' to submit the sequence."<<endl;getline(cin,input);istringstreamiss(input);// Store the itemswhile(iss>>val){temp=newPosition(val);temp->update_index(index);sequence.push_back(*temp);index++;deletetemp;}return;}/* * Computes the longest subsquence */voidcomputeMaxSub(){intlargest_sub=0;Position*temp_parent=NULL;for(vector<Position>::iteratori=sequence.begin();i!=sequence.end();++i){for(intj=0;j<i->get_index();j++){if(i->get_number()>=sequence.at(j).get_number()&&sequence.at(j).get_max()>largest_sub){largest_sub=sequence.at(j).get_max();temp_parent=&sequence.at(j);}}// Update the largest increasing subsequencei->update_parent(temp_parent);i->update_max(largest_sub+1);// Reset the valueslargest_sub=0;temp_parent=NULL;}return;}/* * Outputs the longest subsequence to stdout */voiddisplayResults(){intlargest=0;Position*temp_position;vector<Position>output;for(vector<Position>::iteratori=sequence.begin();i!=sequence.end();i++){if(i->get_max()>largest){largest=i->get_max();temp_position=&*i;}}while(temp_position!=NULL){output.push_back(*temp_position);temp_position=temp_position->get_parent();}cout<<endl<<"The longest subsequence had length: "<<output.front().get_max()<<endl<<endl;while(!output.empty()){cout<<output.back().get_number();if(output.size()>1)cout<<" -> ";elsecout<<endl<<endl;output.pop_back();}return;}