C++ solution inspired by @70664914 with organized explanation
You can find more of my code/explanations in my github repo lzl124631x/LeetCode\n\nIt\'s easy to come up with a brute force solution and to find that there will
*You can find more of my code/explanations in my github repo [lzl124631x/LeetCode](https://github.com/lzl124631x/LeetCode)*\n\nIt\'s easy to come up with a brute force solution and to find that there will be a **repetitive pattern** when matching `S2` through `S1`. The only problem is how to use the repetitive pattern to save computation.\n\n**Fact:**\nIf `s2` repeats in `S1` `R` times, then `S2` must repeats in `S1` `R / n2` times.\n**Conclusion:**\nWe can simply count the repetition of `s2` and then divide the count by `n2`.\n\n**How to denote repetition:**\nWe need to scan `s1` `n1` times. Denote each scanning of `s1` as an `s1` segment.\nAfter each scanning of `i`-th `s1` segment, we will have\n1. The accumulative count of `s2` repeated in this `s1` segment.  \n2. A `nextIndex` that `s2[nextIndex]` is the first letter you\'ll be looking for in the next `s1` segment.\n> Suppose `s1="abc"`, `s2="bac"`, `nextIndex` will be `1`; `s1="abca"`, `s2="bac"`, `nextIndex` will be `2`\n\nIt is the `nextIndex` that is the denotation of the repetitive pattern.\n\n**Example:**\n```\nInput:\ns1="abacb", n1=6\ns2="bcaa", n2=1\n\nReturn:\n3\n```\n```\n                    0 1    2 3 0      1    2 3 0      1    2 3 0  \nS1 --------------> abacb | abacb | abacb | abacb | abacb | abacb \n\nrepeatCount ----->    0  |   1   |   1   |   2   |   2   |   3\n\nIncrement of \nrepeatCount     ->    0  |   1   |   0   |   1   |   0   |   1\n\nnextIndex ------->    2  |   1   |   2   |   1   |   2   |   1\n                                     ^\n\t\t\t\t\t\t\t\t\t |\n\t\t\t\t\t\t\t\t\t repetitive pattern found here (we\'ve met 2 before)!\n\t\t\t\t\t\t\t\t\t The pattern repeated 3 times\n```\n\nThe `nextIndex` has `s2.size()` possible values, ranging from `0` to `s2.size() - 1`. Due to PigeonHole principle, you must find two same `nextIndex` after scanning `s2.size() + 1` `s1` segments.\n\nOnce you meet a `nextIndex` you\'ve met before, you\'ll know that the following `nextIndex`s and increments of `repeatCount` will repeat a pattern.\n\nSo let\'s separate the `s1` segments into 3 parts:\n1. the prefix part before repetitive pattern\n2. the repetitive part\n3. the suffix part after repetitive pattern (incomplete repetitive pattern remnant)\n\nAll you have to do is add up the repeat counts of the 3 parts.\n\n```cpp\n// OJ: https://leetcode.com/problems/count-the-repetitions/\n// Author: github.com/lzl124631x\n// Time: O(|s1| * n1) where |s1| is the length of s1\n// Space: O(n1)\nclass Solution {\npublic:\n    int getMaxRepetitions(string s1, int n1, string s2, int n2) {\n        vector<int> repeatCount(n1 + 1, 0);\n        vector<int> nextIndex(n1 + 1, 0);\n        int j = 0, cnt = 0;\n        for (int k = 1; k <= n1; ++k) {\n            for (int i = 0; i < s1.size(); ++i) {\n                if (s1[i] == s2[j]) {\n                    ++j;\n                    if (j == s2.size()) {\n                        j = 0;\n                        ++cnt;\n                    }\n                }\n            }\n            repeatCount[k] = cnt;\n            nextIndex[k] = j;\n            for (int start = 0; start < k; ++start) {\n                if (nextIndex[start] == j) { // see if you have met this nextIndex before\n                    // if found, you can calculate the 3 parts\n                    int prefixCount = repeatCount[start]; // prefixCount is the start-th repeatCount\n                    // (repeatCount[k] - prefixCount) is the repeatCount of one occurrance of the pattern\n                    // There are (n1 - start) / (k - start) occurrances of the pattern\n                    // So (n1 - start) / (k - start) * (repeatCount[k] - prefixCount) is the repeatCount of the repetitive part\n                    int patternCount = (n1 - start) / (k - start) * (repeatCount[k] - prefixCount);\n                    // The suffix contains the incomplete repetitive remnant (if any)\n                    // Its length is (n1 - start) % (k - start)\n                    // So the suffix repeatCount should be repeatCount[start + (n1 - start) % (k - start)] - prefixCount\n                    int suffixCount = repeatCount[start + (n1 - start) % (k - start)] - prefixCount;\n                    return (prefixCount + patternCount + suffixCount) / n2;\n                }\n            }\n        }\n        return repeatCount[n1] / n2;\n    }\n};\n```\n\n---\n\nUpdate 12/31/2018\n* `int patternCount = (repeatCount[k] - repeatCount[start]) * (n1 - start) / (k - start);` to \n`int patternCount = (repeatCount[k] - repeatCount[start]) * ((n1 - start) / (k - start));` since `a * b / c` doesn\'t necessarily equal `a * (b / c)`. (the old test cases didn\'t cover this case)\n* The size of `repeatCount` and `nextIndex` should be `n1 + 1`.\nThanks for comments from @wxd_sjtu, @rjtsdl, @Rongch\n\nAnother version using `unordered_map` to save computation. Reduce runtime from ~80ms to ~4ms.\n```cpp\n// OJ: https://leetcode.com/problems/count-the-repetitions/\n// Author: github.com/lzl124631x\n// Time: O(|s1| * n1) where |s1| is the length of s1\n// Space: O(n1)\nclass Solution {\npublic:\n    int getMaxRepetitions(string s1, int n1, string s2, int n2) {\n        unordered_map<int, int> kToRepeatCount;\n        unordered_map<int, int> nextIndexToK;\n        kToRepeatCount[0] = 0;\n        nextIndexToK[0] = 0;\n        int j = 0, cnt = 0;\n        for (int k = 1; k <= n1; ++k) {\n            for (int i = 0; i < s1.size(); ++i) {\n                if (s1[i] == s2[j]) {\n                    ++j;\n                    if (j == s2.size()) {\n                        j = 0;\n                        ++cnt;\n                    }\n                }\n            }\n            if (nextIndexToK.find(j) != nextIndexToK.end()) {\n                int start = nextIndexToK[j];\n                int prefixCount = kToRepeatCount[start];\n                int patternCount = (n1 - start) / (k - start) * (cnt - prefixCount);\n                int suffixCount = kToRepeatCount[start + (n1 - start) % (k - start)] - prefixCount;\n                return (prefixCount + patternCount + suffixCount) / n2;\n            }\n            kToRepeatCount[k] = cnt;\n            nextIndexToK[j] = k;\n        }\n        return kToRepeatCount[n1] / n2;\n    }\n};\n```
[]