-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW05_4111056036_1.java
62 lines (58 loc) · 1.89 KB
/
HW05_4111056036_1.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
public class HW05_4111056036_1 extends WordChain {
private static class Position {
int start;
int end;
public Position(int start, int end) {
this.start = start;
this.end = end;
}
}
private static Position greedyFind(String target, String A) {
int[] record = new int[100];
int start = A.indexOf(target);
int end;
int targetLength = target.length();
int dataId = 0;
int max = 0;
Position position = new Position(0, 0);
while(start != -1) {
start += targetLength;
end = A.indexOf(" ", start + 1);
if(end == -1) {
break;
}
for(int i = start; i <= end; ++i) {
dataId += A.charAt(i);
}
dataId %= 100;
if(++record[dataId] > max) {
max = record[dataId];
position.start = start;
position.end = end;
}
dataId = 0;
start = A.indexOf(target, start);
}
return position;
}
@Override
public String sequenceProbability(String[] A) {
StringBuilder sb = new StringBuilder();
sb.append(A[0]);
sb.append(" ");
int oldLength = sb.length();
int newLength;
Position position = greedyFind(sb.toString(), A[1]);
sb.append(A[1].substring(position.start, position.end));
sb.append(" ");
newLength = sb.length();
position = greedyFind(sb.substring(oldLength, newLength), A[1]);
oldLength = newLength;
sb.append(A[1].substring(position.start, position.end));
sb.append(" ");
newLength = sb.length();
position = greedyFind(sb.substring(oldLength, newLength), A[1]);
sb.append(A[1].substring(position.start, position.end));
return sb.toString();
}
}