count-the-repetitions
https://leetcode.com/problems/count-the-repetitions/
下面是我的方法,结果对的,超时了。。。
package com.company;
class Solution {
public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
int len1 = s1.length();
int[][]stores = new int[26][len1];
int[] pos = new int[26];
int[] cur = new int[26];
int index;
for (int i=0; i<len1; i++) {
index = s1.charAt(i)-'a';
stores[index][pos[index]] = i;
pos[index] = pos[index] + 1;
}
int curPos = 0;
int ret = 0;
int len2 = s2.length();
while (true) {
for (int i=0; i<n2; i++) {
for (int j=0; j<len2; j++) {
index = s2.charAt(j) - 'a';
if (cur[index] >= pos[index] * n1) {
return ret;
}
int newPos = 0;
do {
newPos = cur[index] / pos[index] * len1 + stores[index][cur[index] % pos[index]];
cur[index] = cur[index] + 1;
} while (newPos < curPos && cur[index] < pos[index] * n1);
if (newPos < curPos) {
return ret;
}
curPos = newPos + 1;
}
}
ret++;
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
String s1 = "niconiconi";
int n1 = 99981;
String s2 = "nico";
int n2 = 81;
Solution solution = new Solution();
int ret = solution.getMaxRepetitions(s1, n1, s2, n2);
// Your Codec object will be instantiated and called as such:
System.out.printf("ret:%d\n", ret);
System.out.println();
}
}
优化之后的结果,还是超时:
加了string到array的优化,另外每次循环之后坐个判断剪枝。
package com.company;
class Solution {
public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
int len1 = s1.length();
int[][]stores = new int[26][len1];
int[] pos = new int[26];
int[] cur = new int[26];
int index;
for (int i=0; i<len1; i++) {
index = s1.charAt(i)-'a';
stores[index][pos[index]] = i;
pos[index] = pos[index] + 1;
}
int curPos = 0;
int ret = 0;
int len2 = s2.length();
char[] array2 = s2.toCharArray();
while (true) {
for (int i=0; i<n2; i++) {
for (int j=0; j<len2; j++) {
index = array2[j] - 'a';
int newPos = 0;
while (cur[index] < pos[index] * n1) {
newPos = cur[index] / pos[index] * len1 + stores[index][cur[index] % pos[index]];
cur[index] = cur[index] + 1;
if (newPos >= curPos) {
break;
}
}
if (newPos < curPos) {
/*System.out.printf("index %d cur[index] %d pos[index] %d cur/-pos %d, store %d\n",
index, cur[index], pos[index], cur[index] % pos[index], stores[index][cur[index] % pos[index]]);
System.out.printf("newPos %d curPos %d\n",
newPos, curPos);
*/
return ret;
}
curPos = newPos + 1;
}
}
ret++;
for (int i=0; i<26; i++) {
if (pos[i] > 0 && cur[i] >= pos[i] * n1) {
return ret;
}
}
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
String s1 = "acb";
int n1 = 4;
String s2 = "ab";
int n2 = 2;
Solution solution = new Solution();
int ret = solution.getMaxRepetitions(s1, n1, s2, n2);
// Your Codec object will be instantiated and called as such:
System.out.printf("ret:%d\n", ret);
System.out.println();
}
}
用了这种Brute Force的方法,居然比我的快。。。。。。
public class Solution {
public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
char[] array1 = s1.toCharArray(), array2 = s2.toCharArray();
int count1 = 0, count2 = 0, i = 0, j = 0;
while (count1 < n1) {
if (array1[i] == array2[j]) {
j++;
if (j == array2.length) {
j = 0;
count2++;
}
}
i++;
if (i == array1.length) {
i = 0;
count1++;
}
}
return count2 / n2;
}
}
(完)
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 ...
- LeetCode466. Count The Repetitions
题目链接 传送门 题意 定义一个特殊的串, 现在给出串S1和S2的参数, 问: S2最多可以多少个连接起来扔是S1的子序列, 求出这个最大值 解题思路 注意s1与S1的区别, 可以去看题目描述, 预处 ...
随机推荐
- VI 命令简介
1.打开一个文件 vi 文件路径 2.命令模式转换 输入模式 i 命令模式 esc 3.复制 和 粘贴 1)将光标移动到将要复制的行处,按yy进行复制当前行(按nyy复制n行),再移动到粘贴位 ...
- poj1703 Lost Cows
给定集合{1,2,...,n}的一个置换,指定每个位置上在其左方且比其小的数的个数,求该置换. 这题我目前还只会O(n^2)的做法. 以后再用更高效的算法解决. http://poj.org/prob ...
- DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- java中|与||,&与&&到底有什么区别呢?
&是位运算符.&&是布尔逻辑运算符.在运行上,&两边的条件都要判断(不管前面的是ture还是false),而&&先判断前面的,若为false,则后面的不 ...
- Android Studio教程,Android Studio安装教程
http://jingyan.baidu.com/article/67662997393cf654d51b8435.html
- ContentProvider官方教程(2)简介、Content URIs
In this document Overview Accessing a provider Content URIs Content Provider Basics A content provid ...
- 【MySQL】MySQL的Sequence
Oracle的Sequence用爽了,发现MySQL没有Sequence,那么,自己写一个呗. > 最简单的实现 先建一个表存储当前值: CREATE TABLE `t_sequence` ( ...
- Centos6.6上安装mysql5.6中的一些典型问题
经过两天的摸索,终于成功在CentOS6.6系统上成功安装了mysql5.6,现整理如下. (1)安装时的问题: 最小化安装后,安装rpm包时经常会遇到 linux/centos Header V3 ...
- CCNA training notes
5/29: vlan:virtual lan, 通过PVID来将物理上连通的host/PC划分到不同的局域网. switch的每个port有access与trunk两种mode,trunk模式的por ...
- mongodb .net driver
1.介绍 The official MongoDB .NET Driver provides asynchronous interaction with MongoDB. Powering the d ...