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

466. Count The Repetitions的更多相关文章

  1. 第七周 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的第i位开始匹配2^k个 ...

  2. [LeetCode] 466. Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  3. 【leetcode 字符串】466. Count The Repetitions

    https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...

  4. CH5702 Count The Repetitions

    题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...

  5. [LeetCode] Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  6. [Swift]LeetCode466. 统计重复个数 | Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  7. 466. Count Linked List Nodes【Naive】

    Count how many nodes in a linked list. Example Given 1->3->5, return 3. 解法一: /** * Definition ...

  8. Leetcode: Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  9. CH5702 Count The Repetitions[倍增dp]

    http://contest-hunter.org:83/contest/0x50%E3%80%8C%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E3%80%8D%E4%B ...

随机推荐

  1. HDFS NameNode HA 部署文档

    简介: HDFS High Availability Using the Quorum Journal Manager Hadoop 2.x 中,HDFS 组件有三个角色:NameNode.DataN ...

  2. redis在Web中的使用

    redis是一个键值对数据库,用于缓存数据. redis是一个key-value存储系统.和Memcached数据库类似,它支持存储的value类型相对更多,包括string(字符串).list(链表 ...

  3. jdk版本问题

    今天遇到很郁闷的问题jdk 版本是1.6 如何设置1.8 记录一下 可以设置环境变量设置jdk版本问题再就是在 1.java工具设置jdk版本问题 2.grandle  设置要注意 3.生成环境设置j ...

  4. 不同包之间的继承extends

    情景如下: 两个类:ExtendsSuper(父类).ExtendsSub(子类) 两个包:父类ExtendsSuper位于包packSuper路径下,子类ExtendsSub位于包packSub路径 ...

  5. [原创]解读2017 OWASP Top10漏洞体系(含接口安全)

    2017年4月初,OWASP发布了关于Top10的征求意见版. 争议最大的是A7攻击检测与防范不足. 但我主要是按照日常的渗透漏洞进行解读分析的. 解读完毕后,首发t00ls原创文章. https:/ ...

  6. PHPCMS V9 模块开发 二次开发实例 留言本

    鄙人实现了PHPCMS V9 产品开发权威指南(2011官方最新版).doc中的留言板实例,并加上模块安装和卸载功能, 程序可以运行,但只实现基本功能,目的是想让和我一样徘徊在PHPCMS门口不知道从 ...

  7. 图形查询属性(IdentifyTask实现查询)//查询本地服务

    主页代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  8. swift 学习之 UIAlertViewController

    // //  PushViewController.swift //  tab // //  Created by su on 15/12/7. //  Copyright © 2015年 tian. ...

  9. angularjs之事件绑定、解除事件绑定

    今天在开发时,遇到一个坑,花了一下午时间也没找到原因,无奈小菜鸟只能寻求公司里大牛的帮助,果然,大牛就是大牛,对比了几个输出结果,就看出问题所在.所以小菜鸟当然不会错过这个分享的时机啦~废话不多说进入 ...

  10. 23 DesignPatterns学习笔记:C++语言实现 --- 2.1 Bridge

    23 DesignPatterns学习笔记:C++语言实现 --- 2.1 Bridge 2016-07-22 (www.cnblogs.com/icmzn) 模式理解