邓心一 6 سال پیش
والد
کامیت
6b35401534
2فایلهای تغییر یافته به همراه35 افزوده شده و 1 حذف شده
  1. 1 1
      hard/466.count-the-repetitions.go
  2. 34 0
      hard/466.count-the-repetitions.java

+ 1 - 1
hard/466.count-the-repetitions.go

@@ -4,7 +4,7 @@ func getMaxRepetitions(s1 string, n1 int, s2 string, n2 int) int {
 	// from the ith char of s1.
 	// from the ith char of s1.
 	m, n := len(s1), len(s2)
 	m, n := len(s1), len(s2)
 	r1, r2 := []rune(s1), []rune(s2)
 	r1, r2 := []rune(s1), []rune(s2)
-	dp := make([][]int, m+1)
+	dp := make([][]int, m)
 	for i := range dp {
 	for i := range dp {
 		dp[i] = make([]int, 31) // 2^30 is the limit, cauz MaxInt32 < 2^31
 		dp[i] = make([]int, 31) // 2^30 is the limit, cauz MaxInt32 < 2^31
 	}
 	}

+ 34 - 0
hard/466.count-the-repetitions.java

@@ -0,0 +1,34 @@
+class Solution {
+    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
+        char[] chs1 = s1.toCharArray();
+        char[] chs2 = s2.toCharArray();
+        int m = chs1.length;
+        int n = chs2.length;
+        long[][] dp = new long[m][31];
+        // dp[i][k] means the length of s1 required to match 2^k s2,
+        // from the ith char of s1.
+        // dp[i][k] = dp[i][k - 1] + dp[(i + dp[i][k - 1]) % m][k - 1]
+        for (int i = 0; i < m; i++) {
+            int l1 = i, l2 = 0;
+            for (; l2 < n; l1++, l2++)
+                for (; l1 < m * n1 && chs1[l1 % m] != chs2[l2]; l1++);
+            dp[i][0] = l1 - i;
+        }
+        boolean loop = true;
+        for (int k = 1; loop && k < 31; k++) {
+            for (int i = 0; i < m; i++) {
+                long prev = dp[i][k - 1];
+                dp[i][k] = prev + dp[(int) ((i + prev) % m)][k - 1];
+            }
+        }
+        long res = 0L, i = 0L;
+        for (int k = 30; 0 <= k; k--) {
+            long next;
+            while ((next = i + dp[(int) (i % m)][k]) <= m * n1) {
+                res += 1 << k;
+                i = next;
+            }
+        }
+        return (int) (res / n2);
+    }
+}