[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 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 ≤ 106and 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
这道题放了好久才写,主要是因为这道题难度确实不小,光是分析研究网上大神们的帖子就搞了好久,就是现在也不能说完全理解了这道题,哎,将就着写吧,有不足的地方欢迎指正。主要参考了网上 大神 lzl124631x 的帖子,还有 大神 aaaeeeo 的帖子。 这道题的 Java 版本的 brute force 可以通过 OJ,但是 C++ 的就不行了,这里需要使用重复模式来优化,通过分析可知:
如果 s2 在 S1 中出现了N次,那么 S2 肯定在 S1 中出现了 N/n2 次,注意这里的大写表示字符串加上重复次数组成的大字符串。
所以可以得出结论,只要算出 s2 出现的次数,然后除以 n2,就可以得出 S2 出现的次数了。
那么问题就是表示重复,遍历 s1 字符串 n1 次,表示每个 s1 字符串为一段,对于每段,可以得知:
1. 出现在该段的 s2 字符串的累计出现次数
2. 一个 nextIndex,其中 s2[nextIndex] 表示在下一段 s1 中你所要寻找的 s2 中的一个字符。(比如说 s1="abc", s2="bac", 由于第一个 s1 中只能匹配上 s2 中的b,那么只有在下一段 s1 中才能继续匹配 s2 中的a,所以 nextIndex=1,即a在 s2 中的位置为1;同理,比如 s1="abca", s2="bac",第一个 s1 可以匹配上 s2 中的 ba,那么后面的c只能在下一段 s1 中匹配上,那么 nextIndex=2,即c在 s2 中的位置为2)
表示重复关键就在于 nextIndex,比如对于下面这个例子:
Input:
s1="abacb", n1=
s2="bcaa", n2= Return:
j --------------->
S1 --------------> abacb | abacb | abacb | abacb | abacb | abacb repeatCount -----> | | | | | nextIndex -------> | | | | |
nextIndex 的范围从0到 s2.size()-1,根据鸽巢原理(又称抽屉原理),你一定会找到相同的两个 nextIndex 在遍历 s1 段 s2.size()+1 次之后。在上面的例子中,重复的 nextIndex 出现在第三段,和第一段一样都为2,那么重复的 pattern 就找到了,是第二段和第三段中的 aabc,而且从第四段开始,每两段就有一个 aabc,现在的目标就是统计出整个 S1 中有多少个s2。
由于 pattern 占用了两段,所以 interval 为2,然后看整个 S1 中有多少个这样的两段,repeat = (n1 - start) / interval。start 表示 pattern 的起始段数,之前的不是 pattern,然后算在整个 S1 中有多少个 pattern 出现,patternCnt = (repeatCnt[k] - repeatCnt[start]) * repeat,注意这里的 repeatCnt[k] - repeatCnt[start] 表示一个 pattern 中有多少个字符串 s2,个人感觉一般来说都是1个。然后算出剩下的非 pattern 的字符串里能包含几个 s2,remainCnt = repeatCnt[start + (n1 - start) % interval],然后把 patternCnt + remainCnt 之和算出来除以 n2 就是需要的结果啦。如果 pattern 未曾出现,那么我们直接用 repeatCnt[n1] / n2 也能得到正确的结果,参见代码如下:
class Solution {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
vector<int> repeatCnt(n1 + , );
vector<int> nextIdx(n1 + , );
int j = , cnt = ;
for (int k = ; k <= n1; ++k) {
for (int i = ; i < s1.size(); ++i) {
if (s1[i] == s2[j]) {
++j;
if (j == s2.size()) {
j = ;
++cnt;
}
}
}
repeatCnt[k] = cnt;
nextIdx[k] = j;
for (int start = ; start < k; ++start) {
if (nextIdx[start] == j) {
int interval = k - start;
int repeat = (n1 - start) / interval;
int patternCnt = (repeatCnt[k] - repeatCnt[start]) * repeat;
int remainCnt = repeatCnt[start + (n1 - start) % interval];
return (patternCnt + remainCnt) / n2;
}
}
}
return repeatCnt[n1] / n2;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/466
参考资料:
https://leetcode.com/problems/count-the-repetitions/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Count The Repetitions 计数重复个数的更多相关文章
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LeetCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- [Leetcode] count and say 计数和说
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- Leetcode: Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [LeetCode]Count and Say 计数和发言
Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...
- Leetcode 466.统计重复个数
统计重复个数 定义由 n 个连接的字符串 s 组成字符串 S,即 S = [s,n].例如,["abc", 3]="abcabcabc". 另一方面,如果我们可 ...
- Java实现 LeetCode 466 统计重复个数
466. 统计重复个数 定义由 n 个连接的字符串 s 组成字符串 S,即 S = [s,n].例如,["abc", 3]="abcabcabc". 另一方面, ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
随机推荐
- 【目录】JVM目录
JVM学习目录 为了方便园友,现对JVM序列笔记做了归档,园友们可以一口气读完整个JVM的笔记 1. [JVM]JVM系列之JVM体系(一) 2. [JVM]JVM系列之垃圾回收(二) 3. [JVM ...
- ASP.NET Core 中文文档 第一章 入门
原文:Getting Started 翻译:娄宇(Lyrics) 校对:刘怡(AlexLEWIS) 1.安装 .NET Core 2.创建一个新的 .NET Core 项目: mkdir aspnet ...
- jquery遍历table获取td单元格的值
$("#grd").find("tr").each(function () { //第二列单元格的值eq(索引) alert($(this).children( ...
- Yii 2.x RESTful 应用 - 类图
配置url管理器配置请求数据解析器配置用户控制器 ['GET', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']
- Servlet3.0的可插拔功能
如果说 3.0 版本新增的注解支持是为了简化 Servlet/ 过滤器 / 监听器的声明,从而使得 web.xml 变为可选配置, 那么新增的可插性 (pluggability) 支持则将 Servl ...
- PowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作
PowerDesigner的操作经常忘记,所以把常用的功能记录下来备忘. 1.修改反转过来的字段 PowerDesigner从数据库反转的时候,默认不带注释,需要先进行修改. 输入如下脚本: {OWN ...
- ArrayList LinkedList源码解析
在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...
- java文档注释--javadoc的用法
1.前言 Java中有三种注释方式.前两种分别是 // 和 /* */,主要用于代码的注释,以此来方便代码的可读性.第三种被称作说明注释或文档注释,它以 /** 开始,以 */结束,文档注释允许你在程 ...
- LinqToXml (一) Create Xml file By Dom /Linq
目前,在xml 应用编程领域比较流行的开发模型是W3C 提供的DOM(文档对象模型),在.net Framework 通过命名空间 System.Xml 对该技术提供了支持.随着Linq to XMl ...
- transformjs污染了DOM?是你不了解它的强大
原文链接: https://github.com/AlloyTeam/AlloyTouch/wiki/Powerful-transformjs 写在前面 上星期在React微信群里,有小伙伴觉得tra ...