Skip to content

Commit 1835833

Browse files
committed
modify code
1 parent d59fa6d commit 1835833

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/class44/Code02_InsertS2MakeMostAlphabeticalOrder.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public static String right(String s1, String s2) {
2222
return ans;
2323
}
2424

25-
// 正式方法
26-
public static String max(String s1, String s2) {
25+
// 正式方法 O(N+M) + O(M^2)
26+
// N : s1长度
27+
// M : s2长度
28+
public static String maxCombine(String s1, String s2) {
2729
if (s1 == null || s1.length() == 0) {
2830
return s2;
2931
}
@@ -58,13 +60,35 @@ public static String max(String s1, String s2) {
5860
int comp = N + 1;
5961
for (int i = 0; i < N; i++) {
6062
if (rank[i] < rank[comp]) {
61-
int best = whereSplit(s1, s2, i);
63+
int best = bestSplit(s1, s2, i);
6264
return s1.substring(0, best) + s2 + s1.substring(best);
6365
}
6466
}
6567
return s1 + s2;
6668
}
6769

70+
public static int bestSplit(String s1, String s2, int first) {
71+
int N = s1.length();
72+
int M = s2.length();
73+
int end = N;
74+
for (int i = first, j = 0; i < N && j < M; i++, j++) {
75+
if (s1.charAt(i) < s2.charAt(j)) {
76+
end = i;
77+
break;
78+
}
79+
}
80+
String bestPrefix = s2;
81+
int bestSplit = first;
82+
for (int i = first + 1, j = M - 1; i <= end; i++, j--) {
83+
String curPrefix = s1.substring(first, i) + s2.substring(0, j);
84+
if (curPrefix.compareTo(bestPrefix) >= 0) {
85+
bestPrefix = curPrefix;
86+
bestSplit = i;
87+
}
88+
}
89+
return bestSplit;
90+
}
91+
6892
public static class DC3 {
6993

7094
public int[] sa;
@@ -186,20 +210,6 @@ private int[] rank() {
186210

187211
}
188212

189-
public static int whereSplit(String str1, String str2, int first) {
190-
int M = str2.length();
191-
String bestPrefix = str2;
192-
int bestSplit = first;
193-
for (int i = first + 1, j = M - 1; i <= Math.min(str1.length(), first + M); i++, j--) {
194-
String curPrefix = str1.substring(first, i) + str2.substring(0, j);
195-
if (curPrefix.compareTo(bestPrefix) >= 0) {
196-
bestPrefix = curPrefix;
197-
bestSplit = i;
198-
}
199-
}
200-
return bestSplit;
201-
}
202-
203213
// for test
204214
public static String randomNumberString(int len, int range) {
205215
char[] str = new char[len];
@@ -221,7 +231,7 @@ public static void main(String[] args) {
221231
String s1 = randomNumberString(s1Len, range);
222232
String s2 = randomNumberString(s2Len, range);
223233
String ans1 = right(s1, s2);
224-
String ans2 = max(s1, s2);
234+
String ans2 = maxCombine(s1, s2);
225235
if (!ans1.equals(ans2)) {
226236
System.out.println("Oops!");
227237
System.out.println(s1);
@@ -236,11 +246,11 @@ public static void main(String[] args) {
236246
System.out.println("性能测试开始");
237247

238248
int s1Len = 1000000;
239-
int s2Len = 50;
249+
int s2Len = 500;
240250
String s1 = randomNumberString(s1Len, range);
241251
String s2 = randomNumberString(s2Len, range);
242252
long start = System.currentTimeMillis();
243-
max(s1, s2);
253+
maxCombine(s1, s2);
244254
long end = System.currentTimeMillis();
245255
System.out.println("运行时间 : " + (end - start) + " ms");
246256
System.out.println("性能测试结束");

0 commit comments

Comments
 (0)