-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundA.java
More file actions
78 lines (78 loc) · 2.54 KB
/
RoundA.java
File metadata and controls
78 lines (78 loc) · 2.54 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.*;
public class RoundA{
public static int max(int a,int b,int c){
return a>b&&a>c?a:b>c?b:c;
}
public static void printSolution(char[][] sol,int row,int col,char[] S){
int i=row-1,j=col-1;
while(i!=0 && j!=0){
if(sol[i][j]=='\\'){
System.out.print(S[i-1]);
i--;
j--;
}else if(sol[i][j]=='|'){
i--;
}
else{
j--;
}
}
System.out.println();
}
public static int Solution(String s){
int n=s.length();
char[] S1=s.toCharArray();
char[] S2=new char[n];
for(int i=n-1,j=0;i>=0 && j<n;i--,j++){
S2[i]=S1[j];
}
int[][] dp=new int[n+1][n+1];
char[][] sol=new char[n+1][n+1];
for(int i=0;i<n+1;i++){
for(int j=0;j<n+1;j++){
if(i==0 || j==0){
dp[i][j]=0;
sol[i][j]='\0';
}else if(S1[i-1]==S2[j-1]){
char ch1=i-2<0?'\0':S1[i-2];
char ch2=j-2<0?'\0':S2[j-2];
if(ch1!='\0' && ch2!='\0'){
if(ch1==ch2){
dp[i][j]=dp[i-1][j-1]+1;
sol[i][j]='\\';
}else{
dp[i][j]=max(dp[i][j-1],dp[i-1][j],dp[i-1][j-1]+1);
if(dp[i][j]==dp[i][j-1]){
sol[i][j]='-';
}else if(dp[i][j]==dp[i-1][j]){
sol[i][j]='|';
}else{
sol[i][j]='\\';
}
}
}else{
dp[i][j]=dp[i-1][j-1]+1;
sol[i][j]='\\';
}
}
else{
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
if(dp[i][j]==dp[i-1][j]){
sol[i][j]='|';
}else{
sol[i][j]='-';
}
}
}
}
printSolution(sol,n+1,n+1,S1);
return dp[n][n];
}
public static void main(String[] args) {
String s;
Scanner sc=new Scanner(System.in);
s=sc.next();
System.out.println(Solution(s));
sc.close();
}
}