zoukankan      html  css  js  c++  java
  • 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 that it becomes s1. For example, “abc” can be obtained from “abdbec” based on our definition, but it can not be obtained from “acbbe”.

    You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106 and 1 ≤ n2 ≤ 106. Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2]. Find the maximum integer M such that [S2,M] can be obtained from S1.

    Example:

    Input:
    s1="acb", n1=4
    s2="ab", n2=2
    
    Return:
    2

    Approach #1: string. [C++]

    class Solution {
    public:
        int getMaxRepetitions(string s1, int n1, string s2, int n2) {
            vector<int> repeatCount(n1+1, 0);
            vector<int> nextIndex(n1+1, 0);
            int j = 0, cnt = 0;
            for (int k = 1; k <= n1; ++k) {
                for (int i = 0; i < s1.size(); ++i) {
                    if (s1[i] == s2[j]) ++j;
                    if (j == s2.size()) {
                        j = 0;
                        ++cnt;
                    }
                }
                repeatCount[k] = cnt;
                nextIndex[k] = j;
                for (int start = 0; start < k; ++start) {
                    if (nextIndex[start] == j) {
                        int prefixCount = repeatCount[start];
                        int patternCount = (n1 - start) / (k - start) * (repeatCount[k] - prefixCount);
                        int suffixCount = repeatCount[start + (n1 - start) % (k - start)] - prefixCount;
                        return (prefixCount + patternCount + suffixCount) / n2;
                    }
                }
            }
            
            return repeatCount[n1] / n2;
        }
    };
    

      

    Analysis:

    Fact:

    If s2 repeats in S1 R times, then S2 must repeats in S1 R / n2 times.

    Conclusion:

    We can simply count the repeation of s2 and then divide the count by n2.

    How to denote repeatition:

    We need to scan s1 n1 times. Denote each scanning of s1 as an s1 segment.

    After each scanning of i-th s1 segment, we will have the accumulative count of s2 repeated in this s1 segment.

    A nextIndex that s2[nextIndex] is the first letter you'll be looking for in the next s1 segment. 

    Suppose s1="abc", s2="bac", nextIndex will be 1; s1="abca", s2="bac", nextIndex will be 2.

    It is the nextIndex that is the denotation of the repetitive pattern.

    Example:

    Input:

    s1 = "abacb", n1 = 6

    s2 = "bcaa", n2 = 1

    Return:

    3

               0  1   2 3  0    1     2  3 0    1  2  3  0
      S1 --------------> abacb | abacb | abacb | abacb | abacb | abacb

    repeatCount ----->    0   |   1  |      1 |  2  |    2 |     3

    nextIndex ------->    2     |    1 |   2 |      1 |    2 |     1

            

    Once you meet a nextIndex you've met before, you'll know that the following nextIndex ans increments of repeatCount will repeat a pattern.

    So let's seperate the s1 segments into 3 parts:

    the prefix part before repetitive pattern

    the repetitive part

    the suffix part after repetitive pattertn (incomplete pattern remnant).

    Reference:

    https://leetcode.com/problems/count-the-repetitions/discuss/95398/C%2B%2B-solution-inspired-by-%4070664914-with-organized-explanation

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    《C++ Primer》笔记string,vector
    《C++ Primer》笔记创建动态数组new,delete
    《C++ Primer》笔记迭代器iterator
    浅析jQuery基础框架
    linux常用命令
    Javascript常用对象详细讲解
    Javascript兼容多种浏览器
    Vim常用命令
    提高 web 应用性能之 JavaScript 性能调优
    jQuery LigerUI使用教程入门篇
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10480218.html
Copyright © 2011-2022 走看看