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. Spring Data JPA 基本使用

    Spring Data 简化开发,支持Nosql和关系型数据库, DEMO https://github.com/easonstudy/boot-demo/tree/master/boot-sprin ...

  2. Tutorial: Getting Started with Spring Security

    This tutorial will cover a basic scenario where it  integrates Spring Security, using database-backe ...

  3. zookeeper无故挂掉,启动不起来的问题

    一台测试的服务器重启后,运行zookeeperd的启动脚本显示启动成功: [plain] view plain copy     JMX enabled by default Using config ...

  4. 7.25 6figting!

    TEXT 82 Proton 马来西亚宝腾汽车 A fork in the road 何去何从?(陈继龙编译) Nov 30th 2006 | HONG KONG From The Economist ...

  5. 迷你MVVM框架 avalonjs 1.2.5发布

    avalon1.2.5发布,升级ms-widget,整合avalon.require.text到核心,并且修复了avalon.mobile的avalon.innerHTML方法的BUG,让它能执行脚本 ...

  6. Spring IOC容器启动流程源码解析(一)——容器概念详解及源码初探

    目录 1. 前言 1.1 IOC容器到底是什么 1.2 BeanFactory和ApplicationContext的联系以及区别 1.3 解读IOC容器启动流程的意义 1.4 如何有效的阅读源码 2 ...

  7. 数据库帮助类 SqlServerHelp

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  8. sql server 设置用户名和密码

    安全性:security 登录名:Logins 可以双击直接对里面现有的用户权限进行修改 也可以右击新建新用户 并对新用户权限进行设置

  9. C语言Web service编程

    一.简介 Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用 ...

  10. Python爬虫进阶四之PySpider的用法

    审时度势 PySpider 是一个我个人认为非常方便并且功能强大的爬虫框架,支持多线程爬取.JS动态解析,提供了可操作界面.出错重试.定时爬取等等的功能,使用非常人性化. 本篇内容通过跟我做一个好玩的 ...