LeetCode466. Count The Repetitions
- 题目链接
- 题意
- 定义一个特殊的串, 现在给出串S1和S2的参数, 问: S2最多可以多少个连接起来扔是S1的子序列, 求出这个最大值
- 解题思路
- 注意s1与S1的区别, 可以去看题目描述, 预处理出s1从s2的每一个字符开始匹配, s1可以匹配到s2串尾的数量, 即如果从s2的当前下标开始匹配, 这一个s1最多可以匹配多少s2, 这个s1匹配完成后匹配到了s2的哪一个字符, 下一个s1匹配s2的话应该从哪一个下标开始, 因为串长最长100, 这个预处理时间消耗是很少的, 有了这个预处理的结果, 我们就可以O(n)的知道S1可以匹配多少个s2, 进而推算出匹配多少个S2.
- AC代码
class Solution {
public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
int answer = 0; ArrayList<Integer> numArrayList = new ArrayList<Integer>();
ArrayList<Integer> idxArrayList = new ArrayList<Integer>(); for(int i = 0; i < s2.length(); ++i) {
int num = 0, idx = i;
for(int j = 0; j < s1.length(); ++j) {
if(s2.charAt(idx) == s1.charAt(j)) {
idx++;
if(idx == s2.length()) {
num++;
idx = 0;
}
}
}
numArrayList.add(num);
idxArrayList.add(idx);
} int idx = 0; for(int i = 0; i < n1; ++i) {
answer += numArrayList.get(idx);
idx = idxArrayList.get(idx);
} return answer / n2;
}
}
LeetCode466. Count The Repetitions的更多相关文章
- CH5702 Count The Repetitions
题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...
- [Swift]LeetCode466. 统计重复个数 | Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- 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 The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- Leetcode: Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- 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 ...
- 【leetcode 字符串】466. Count The Repetitions
https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...
- 第七周 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个 ...
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
随机推荐
- 浅谈SQL Server、MySQL中char,varchar,nchar,nvarchar区别
最近一次的面试中,被面试官问到varchar和nvarchar的区别,脑海里记得是定长和可变长度的区别,但却没能说出来.后来,在网上找了下网友总结的区别.在这里做个备忘录: 一,SQL Server中 ...
- 模块 jieba结巴分词库 中文分词
jieba结巴分词库 jieba(结巴)是一个强大的分词库,完美支持中文分词,本文对其基本用法做一个简要总结. 安装jieba pip install jieba 简单用法 结巴分词分为三种模式:精确 ...
- D - 淡黄的长裙 HDU - 4221(贪心)
D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...
- Round 1A 2020 - Code Jam 2020
Problem A. Pattern Matching 把每个字符串分成第一个之前,最后一个之后,中间的部分 三个部分 每个字符串的中间的部分可以直接拼接 前后两个部分需要判断下是否合法 #inclu ...
- 使用systemctl工具
使用systemctl工具 8.1问题 本例要求掌握systemctl控制工具的基本操作,完成下列任务: 重启httpd.crond.bluetooth服 ...
- docker win10 基本指令
一.镜像操作 docker images 本地镜像 docker pull imagename 获取网上获取镜像 docker run 创建docker容器 docker rmi imagename ...
- Linux系统安装Dos系统(虚拟机里装)
结合以下两篇优秀的文章就能完成任务. 1.https://www.jb51.net/os/609411.html 2.http://blog.51cto.com/6241809/1687361 所需要 ...
- String、StringBuffer、StringBuilder葫芦三兄弟
今年因为疫情的原因,本打算在读研期间好好做项目,写论文,在今年9月份能找个好工作,但现在迟迟不能开学,也无法正常的给导师打工,所以干脆就打算好好准备工(fan)作(wan)的事儿. 接触Java也有好 ...
- json === dict
import requests import json ''' json.loads(json_str) json字符串转换成字典 json.dumps(dict) 字典转换成json字符串 ''' ...
- django创建app
前几天,