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 ...
随机推荐
- 有个原则就是实体类还是controller入参都应该是 包装类型
问题说明 我在使用JPA作为项目的ORM框架的时候,在分页查询中,不管咋样使用查询不出来数据,然后发现Hibernate构建的查询SQL中,在where子句中带上了createTime=0这个条件.这 ...
- 12c OCR corrupted results in CRS stack down.
12c OCR corrupted results in CRS stack down. 1. check crsd.trc2017-03-21 16:14:44.667838 : CRSOCR:2 ...
- SQL Server中STATISTICS IO物理读和逻辑读的误区
SQL Server中STATISTICS IO物理读和逻辑读的误区 大家知道,SQL Server中可以利用下面命令查看某个语句读写IO的情况 SET STATISTICS IO ON 那么这个命令 ...
- Azure SDK for Python Url
Azure SDK for Python URL 明细表 AZURE_PUBLIC_CLOUD = Cloud( 'AzureCloud', endpoints=CloudEndpoints( man ...
- mui在vue_cli上使用
在main.js里添加 import mui from './assets/js/mui.js' 如果不添加下面会显示mui is not defined 报错 Vue.prototype.mui = ...
- vscode 保存自动 格式化eslint 代码
在网上找了很多种方法,大多都没有成功 一下是一种成功的 配置方法: 1) First, you need to install the ESLint extension in the VS code ...
- c期末笔记(2)
1.定义数组 1.1.a[3][2] = [1,2,3,4,5,6],代码是定义一个三行两列的二维数组.在数组声明和初始化时,如果用户定义的元素数量超过用户规定的元素数量,以语法错误报错.(如:cah ...
- 【STM32项目笔记】STM32CubeMX+Keil+Proteus联合实现LED闪烁
摘要 利用STM32CubeMx配置STM32芯片的功能,然后将配置后的内容生成代码,并导出成可以使用Keil打开编辑的文件,在Keil中添加控制代码后,下载到Proteus仿真中,使用仿真观察代码执 ...
- 安装一个KVM服务器
安装一个KVM服务器 案例1:安装一个KVM服务器 案例2:KVM平台构建及 ...
- IO 模型知多少
1. 引言 同步异步I/O,阻塞非阻塞I/O是程序员老生常谈的话题了,也是自己一直以来懵懵懂懂的一个话题.比如:何为同步异步?何为阻塞与非阻塞?二者的区别在哪里?阻塞在何处?为什么会有多种IO模型,分 ...