@@ -22,8 +22,10 @@ public static String right(String s1, String s2) {
2222return 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 ) {
2729if (s1 == null || s1 .length () == 0 ) {
2830return s2 ;
2931}
@@ -58,13 +60,35 @@ public static String max(String s1, String s2) {
5860int comp = N + 1 ;
5961for (int i = 0 ; i < N ; i ++) {
6062if (rank [i ] < rank [comp ]) {
61- int best = whereSplit (s1 , s2 , i );
63+ int best = bestSplit (s1 , s2 , i );
6264return s1 .substring (0 , best ) + s2 + s1 .substring (best );
6365}
6466}
6567return 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+
6892public static class DC3 {
6993
7094public 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
204214public static String randomNumberString (int len , int range ) {
205215char [] str = new char [len ];
@@ -221,7 +231,7 @@ public static void main(String[] args) {
221231String s1 = randomNumberString (s1Len , range );
222232String s2 = randomNumberString (s2Len , range );
223233String ans1 = right (s1 , s2 );
224- String ans2 = max (s1 , s2 );
234+ String ans2 = maxCombine (s1 , s2 );
225235if (!ans1 .equals (ans2 )) {
226236System .out .println ("Oops!" );
227237System .out .println (s1 );
@@ -236,11 +246,11 @@ public static void main(String[] args) {
236246System .out .println ("性能测试开始" );
237247
238248int s1Len = 1000000 ;
239- int s2Len = 50 ;
249+ int s2Len = 500 ;
240250String s1 = randomNumberString (s1Len , range );
241251String s2 = randomNumberString (s2Len , range );
242252long start = System .currentTimeMillis ();
243- max (s1 , s2 );
253+ maxCombine (s1 , s2 );
244254long end = System .currentTimeMillis ();
245255System .out .println ("运行时间 : " + (end - start ) + " ms" );
246256System .out .println ("性能测试结束" );
0 commit comments