-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
82 lines (75 loc) · 2.49 KB
/
main.java
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
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("the broken keys are:" +func("7_This_is_a_test", "_hs_s_a_es"));
}
/* characters typed:
* 7_This_is_a_test
* ---------------
* characters on screen:
* _hs_s_a_es
*/
static public String func(String input, String output) {
String missingPart = "";
String inputUpcase = input.toUpperCase();
String outputUpcase = output.toUpperCase();
String longestSubString = MyLCS(inputUpcase, outputUpcase);
while (longestSubString.compareTo(inputUpcase) != 0) {
int posStart = inputUpcase.indexOf(longestSubString);
// System.out.println(" ================== ");
System.out.println("posStart = " + posStart + ",lenth = "
+ longestSubString.length());
// System.out.println("longestSubString = " + longestSubString);
// System.out.println(" ================== ");
if (posStart + longestSubString.length() < inputUpcase.length()) {
if (outputUpcase.indexOf(inputUpcase.charAt(posStart
+ longestSubString.length())) < 0
&& missingPart
.indexOf(inputUpcase.charAt(posStart - 1)) < 0) {
missingPart += inputUpcase.charAt(posStart
+ longestSubString.length());
}
longestSubString += inputUpcase.charAt(posStart
+ longestSubString.length());
}
// System.out.println("longestSubString = " + longestSubString);
if (posStart > 0) {
longestSubString = inputUpcase.charAt(posStart - 1)
+ longestSubString;
if (outputUpcase.indexOf(inputUpcase.charAt(posStart - 1)) < 0
&& missingPart
.indexOf(inputUpcase.charAt(posStart - 1)) < 0) {
missingPart += inputUpcase.charAt(posStart - 1);
}
}
// System.out.println("longestSubString = " + longestSubString);
// longestSubString = MyLCS(inputUpcase, outputUpcase);
// System.out.println(" ================== ");
}
return missingPart;
}
public static String MyLCS(String s1, String s2) {
// if (String.IsNullOrEmpty(s1) || String.IsNullOrEmpty(s2))
// {
// return null;
// }
// else
if (s1 == s2) {
return s1;
}
int length = 0;
int end = 0;
int a[][] = new int[s1.length()][s2.length()];
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
int n = (i - 1 >= 0 && j - 1 >= 0) ? a[i - 1][j - 1] : 0;
a[i][j] = s1.charAt(i) == s2.charAt(j) ? 1 + n : 0;
if (a[i][j] > length) {
length = a[i][j];
end = i;
}
}
}
return s1.substring(end - length + 1, end + 1);
}
}