count-the-repetitions

https://leetcode.com/problems/count-the-repetitions/

下面是我的方法,结果对的,超时了。。。

package com.company;

class Solution {
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
        int len1 = s1.length();
        int[][]stores = new int[26][len1];
        int[] pos = new int[26];
        int[] cur = new int[26];
        int index;

        for (int i=0; i<len1; i++) {
            index = s1.charAt(i)-‘a‘;
            stores[index][pos[index]] = i;
            pos[index] = pos[index] + 1;
        }

        int curPos = 0;
        int ret = 0;
        int len2 = s2.length();
        while (true) {

            for (int i=0; i<n2; i++) {
                for (int j=0; j<len2; j++) {
                    index = s2.charAt(j) - ‘a‘;

                    if (cur[index] >= pos[index] * n1) {

                        return ret;
                    }

                    int newPos = 0;
                    do {
                        newPos = cur[index] / pos[index] * len1 + stores[index][cur[index] % pos[index]];
                        cur[index] = cur[index] + 1;
                    } while (newPos < curPos && cur[index] < pos[index] * n1);

                    if (newPos < curPos) {
                        return ret;
                    }
                    curPos = newPos + 1;

                }
            }
            ret++;

        }

    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        String s1 = "niconiconi";
        int n1 = 99981;
        String s2 = "nico";
        int n2 = 81;

        Solution solution = new Solution();
        int ret  = solution.getMaxRepetitions(s1, n1, s2, n2);

        // Your Codec object will be instantiated and called as such:
        System.out.printf("ret:%d\n", ret);

        System.out.println();

    }

}

优化之后的结果,还是超时:

加了string到array的优化,另外每次循环之后坐个判断剪枝。

package com.company;

class Solution {
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
        int len1 = s1.length();
        int[][]stores = new int[26][len1];
        int[] pos = new int[26];
        int[] cur = new int[26];
        int index;

        for (int i=0; i<len1; i++) {
            index = s1.charAt(i)-‘a‘;
            stores[index][pos[index]] = i;
            pos[index] = pos[index] + 1;
        }

        int curPos = 0;
        int ret = 0;
        int len2 = s2.length();
        char[] array2 = s2.toCharArray();
        while (true) {

            for (int i=0; i<n2; i++) {
                for (int j=0; j<len2; j++) {
                    index = array2[j] - ‘a‘;

                    int newPos = 0;
                    while (cur[index] < pos[index] * n1) {
                        newPos = cur[index] / pos[index] * len1 + stores[index][cur[index] % pos[index]];
                        cur[index] = cur[index] + 1;
                        if (newPos >= curPos) {
                            break;
                        }
                    }

                    if (newPos < curPos) {
                        /*System.out.printf("index %d cur[index] %d pos[index] %d cur/-pos %d, store %d\n",
                                index, cur[index], pos[index], cur[index] % pos[index], stores[index][cur[index] % pos[index]]);

                        System.out.printf("newPos %d curPos %d\n",
                                newPos, curPos);
                                */
                        return ret;
                    }
                    curPos = newPos + 1;

                }
            }
            ret++;
            for (int i=0; i<26; i++) {
                if (pos[i] > 0 && cur[i] >= pos[i] * n1) {
                    return ret;
                }
            }

        }

    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        String s1 = "acb";
        int n1 = 4;
        String s2 = "ab";
        int n2 = 2;

        Solution solution = new Solution();
        int ret  = solution.getMaxRepetitions(s1, n1, s2, n2);

        // Your Codec object will be instantiated and called as such:
        System.out.printf("ret:%d\n", ret);

        System.out.println();

    }

}

用了这种Brute Force的方法,居然比我的快。。。。。。

public class Solution {
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
        char[] array1 = s1.toCharArray(), array2 = s2.toCharArray();
        int count1 = 0, count2 = 0, i = 0, j = 0;

        while (count1 < n1) {
            if (array1[i] == array2[j]) {
                j++;
                if (j == array2.length) {
                    j = 0;
                    count2++;
                }
            }
            i++;
            if (i == array1.length) {
                i = 0;
                count1++;
            }
        }

        return count2 / n2;
    }
}

(完)

时间: 2024-10-06 05:45:02

count-the-repetitions的相关文章

466. Count The Repetitions

Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc". On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such th

LeetCode 466 - Count The Repetitions - Hard ( Python)

Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc". On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such th

第七周 Leetcode 466. Count The Repetitions 倍增DP (HARD)

Leetcode 466 直接给出DP方程 dp[i][k]=dp[i][k-1]+dp[(i+dp[i][k-1])%len1][k-1]; dp[i][k]表示从字符串s1的第k位开始匹配2^k个s2串需要的长度 最后通过一个循环 累积最多可以匹配多少个s2串 除以n2下取整就是答案 用倍增加速后 总的复杂度nlogn 而本题的n非常小 轻松AC 体会到倍增的魅力了吧. const int maxn=100+1,INF=1e+9; long long int dp[maxn][30]; cl

Leetcode: Count The Repetitions

Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc". On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such th

【leetcode 字符串】466. Count The Repetitions

https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandyang/p/6149294.html 原文地址:https://www.cnblogs.com/itcsl/p/9061427.html

继续过Hard题目.0209

http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

练练脑,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

MySQL 处理重复数据

有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 本博文我们将为大家介绍如何防止数据表出现重复数据及如何删除数据表中的重复数据. 防止表中出现重复数据 你可以在MySQL数据表中设置指定的字段为 PRIMARY KEY(主键) 或者 UNIQUE(唯一) 索引来保证数据的唯一性. 让我们尝试一个实例:下表中无索引及主键,所以该表允许出现多条重复记录. CREATE TABLE person_tbl ( first_name CH

继续过Hard题目

接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 L

练几道,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca