-
Notifications
You must be signed in to change notification settings - Fork 0
/
Increasing Sub Sequence .txt
60 lines (58 loc) · 1.25 KB
/
Increasing Sub Sequence .txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
int T=Integer.parseInt(br.readLine());
while(T-->0)
{
String s=br.readLine();
char[] sarr=s.toCharArray();
int[] dp=new int[s.length()];
for(int i=0;i<dp.length;i++)
{
dp[i]=-1;
}
int ans=solve(sarr,s.length()-1,dp);
for(int i=0;i<dp.length;i++)
{
if(ans<dp[i])
{
ans=dp[i];
}
}
sb.append(ans+"\n");
}
System.out.print(sb);
}
public static int solve(char[] sarr,int n,int[] dp)
{
if(n<0)
{
return 0;
}
if(dp[n]!=-1)
{
return dp[n];
}
int ans=1;
for(int i=0;i<n;i++)
{
if(sarr[i]<sarr[n])
{
int smalloutput=solve(sarr,i,dp)+1;
if(ans<smalloutput)
{
ans=smalloutput;
}
}
}
dp[n]=ans;
solve(sarr,n-1,dp);
return ans;
}
}