import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Created by Shreyans on 4/16/2015
*/
class LNG_INC_SUB//Longest Increasing Subsequence
{
{
System.
out.
println("Enter Numbers Separated by Spaces to find their LIS\n"); String[] s1
=br.
readLine().
split(" "); int n=s1.length;
int[] a=new int[n];//Array actual of Numbers
String []ls
=new String[n
];// Array of Strings to maintain LIS for every element for(int i=0;i<n;i++)
{
}
int[]dp=new int[n];//Storing length of max subseq.
int max=dp[0]=1;//Defaults
String seq
=ls
[0]=s1
[0];//Defaults for(int i=1;i<n;i++)
{
dp[i]=1;
for(int j=i-1;j>=0;j--)
{
//First check if number at index j is less than num at i.
// Second the length of that DP should be greater than dp[i]
// -1 since dp of previous could also be one. So we compare the dp[i] as empty initially
if(a[j]<a[i]&&dp[j]>dp[i]-1)
{
dp[i]=dp[j]+1;//Assigning temp length of LIS. There may come along a bigger LIS of a future a[j]
x=ls[j];//Assigning temp LIS of a[j]. Will append a[i] later on
}
}
x+=(" "+a[i]);
ls[i]=x;
if(dp[i]>max)
{
max=dp[i];
seq=ls[i];
}
}
System.
out.
println("Length of LIS is: " + max
+ "\nThe Sequence is: " + seq
); }
}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqYXZhLmlvLklucHV0U3RyZWFtUmVhZGVyOwoKLyoqCiAqIENyZWF0ZWQgYnkgU2hyZXlhbnMgb24gNC8xNi8yMDE1CiAqLwoKY2xhc3MgTE5HX0lOQ19TVUIvL0xvbmdlc3QgSW5jcmVhc2luZyBTdWJzZXF1ZW5jZQp7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgRXhjZXB0aW9uCiAgICB7CiAgICAgICAgQnVmZmVyZWRSZWFkZXIgYnI9bmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBJbnB1dFN0cmVhbVJlYWRlcihTeXN0ZW0uaW4pKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkVudGVyIE51bWJlcnMgU2VwYXJhdGVkIGJ5IFNwYWNlcyB0byBmaW5kIHRoZWlyIExJU1xuIik7CiAgICAgICAgU3RyaW5nW10gczE9YnIucmVhZExpbmUoKS5zcGxpdCgiICIpOwogICAgICAgIGludCBuPXMxLmxlbmd0aDsKICAgICAgICBpbnRbXSBhPW5ldyBpbnRbbl07Ly9BcnJheSBhY3R1YWwgb2YgTnVtYmVycwogICAgICAgIFN0cmluZyBbXWxzPW5ldyBTdHJpbmdbbl07Ly8gQXJyYXkgb2YgU3RyaW5ncyB0byBtYWludGFpbiBMSVMgZm9yIGV2ZXJ5IGVsZW1lbnQKICAgICAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICAgIHsKICAgICAgICAgICAgYVtpXT1JbnRlZ2VyLnBhcnNlSW50KHMxW2ldKTsKICAgICAgICB9CiAgICAgICAgaW50W11kcD1uZXcgaW50W25dOy8vU3RvcmluZyBsZW5ndGggb2YgbWF4IHN1YnNlcS4KICAgICAgICBpbnQgbWF4PWRwWzBdPTE7Ly9EZWZhdWx0cwogICAgICAgIFN0cmluZyBzZXE9bHNbMF09czFbMF07Ly9EZWZhdWx0cwogICAgICAgIGZvcihpbnQgaT0xO2k8bjtpKyspCiAgICAgICAgewogICAgICAgICAgICBkcFtpXT0xOwogICAgICAgICAgICBTdHJpbmcgeD0iIjsKICAgICAgICAgICAgZm9yKGludCBqPWktMTtqPj0wO2otLSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgLy9GaXJzdCBjaGVjayBpZiBudW1iZXIgYXQgaW5kZXggaiBpcyBsZXNzIHRoYW4gbnVtIGF0IGkuCiAgICAgICAgICAgICAgICAvLyBTZWNvbmQgdGhlIGxlbmd0aCBvZiB0aGF0IERQIHNob3VsZCBiZSBncmVhdGVyIHRoYW4gZHBbaV0KICAgICAgICAgICAgICAgIC8vIC0xIHNpbmNlIGRwIG9mIHByZXZpb3VzIGNvdWxkIGFsc28gYmUgb25lLiBTbyB3ZSBjb21wYXJlIHRoZSBkcFtpXSBhcyBlbXB0eSBpbml0aWFsbHkKICAgICAgICAgICAgICAgIGlmKGFbal08YVtpXSYmZHBbal0+ZHBbaV0tMSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBkcFtpXT1kcFtqXSsxOy8vQXNzaWduaW5nIHRlbXAgbGVuZ3RoIG9mIExJUy4gVGhlcmUgbWF5IGNvbWUgYWxvbmcgYSBiaWdnZXIgTElTIG9mIGEgZnV0dXJlIGFbal0KICAgICAgICAgICAgICAgICAgICB4PWxzW2pdOy8vQXNzaWduaW5nIHRlbXAgTElTIG9mIGFbal0uIFdpbGwgYXBwZW5kIGFbaV0gbGF0ZXIgb24KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICB4Kz0oIiAiK2FbaV0pOwogICAgICAgICAgICBsc1tpXT14OwogICAgICAgICAgICBpZihkcFtpXT5tYXgpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIG1heD1kcFtpXTsKICAgICAgICAgICAgICAgIHNlcT1sc1tpXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkxlbmd0aCBvZiBMSVMgaXM6ICIgKyBtYXggKyAiXG5UaGUgU2VxdWVuY2UgaXM6ICIgKyBzZXEpOwogICAgfQp9