-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS_Rimi_Das_rimidas.java
More file actions
164 lines (143 loc) · 3.48 KB
/
Copy pathLCS_Rimi_Das_rimidas.java
File metadata and controls
164 lines (143 loc) · 3.48 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class LCS_Rimi_Das_rimidas {
int[][] opt = new int[1000][1000];
char[][] optChar = new char[1000][1000];
char[] finalCharArr = new char[1000];
static String string1;
static String string2;
static File outputFile = null;
private static BufferedWriter bw;
public void FileWriter() {
outputFile = new File("output.txt");
if (!outputFile.exists()) {
try {
outputFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void fileReader(String filePath) {
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
String sCurrentLine;
int line_num = 0;
while ((sCurrentLine = br.readLine()) != null) {
String lineText = sCurrentLine;
if(line_num == 0)
string1 = lineText;
else
string2 = lineText;
line_num++;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void writeToFile(BufferedWriter bw2, String string) {
try {
bw.write(string);
} catch (IOException e) {
e.printStackTrace();
}
}
public enum optChar_Symbols {
SideUp('s'), Left('l'), Up('u');
public char asChar() {
return asChar;
}
private final char asChar;
private optChar_Symbols(char asChar) {
this.asChar = asChar;
}
}
public void getLongestSubSequence(String a, String b){
StringBuilder str = new StringBuilder();
int n = a.length();
int m = b.length();
char[] array_A = a.toCharArray();
char[] array_B = b.toCharArray();
for(int j =0;j<=m; j++)
{
opt[0][j] = 0;
}
for(int i = 1;i<=n;i++)
{
opt[i][0] = 0;
for(int j =1;j<=m; j++)
{
if(array_A[i-1]== array_B[j-1])
{
opt[i][j] = opt[i-1][j-1] +1;
optChar[i][j] = optChar_Symbols.SideUp.asChar;
}
else if(opt[i][j-1] >= opt[i-1][j])
{
opt[i][j] = opt[i][j-1];
optChar[i][j] = optChar_Symbols.Left.asChar;
}
else
{
opt[i][j] = opt[i-1][j];
optChar[i][j] = optChar_Symbols.Up.asChar;
}
}
}
//final traversal
int c = 0;
int i=a.length();
int j=b.length();
while(i>=1 && j>=1)
{
if(optChar[i][j]==optChar_Symbols.SideUp.asChar)
{
//If equal,the add that character to a final rray of characters
finalCharArr[c] = array_A[i-1];
i--;
j--;
c++;
}
else if(optChar[i][j]==optChar_Symbols.Left.asChar)
{
j--;
}
else if(optChar[i][j]==optChar_Symbols.Up.asChar)
{
i--;
}
}
//reversing the final charcter rray and building String
writeToFile(bw, Integer.toString(c));
writeToFile(bw, "\n");
for(i = c-1; i>=0;i--)
str.append(finalCharArr[i]);
writeToFile(bw, str.toString());
//return str.toString();
}
public static void main(String[] args) {
LCS_Rimi_Das_rimidas lcs = new LCS_Rimi_Das_rimidas();
lcs.fileReader("input.txt");
lcs.FileWriter();
lcs.getLongestSubSequence(string1,string2);
try {
bw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}