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的更多相关文章

  1. CH5702 Count The Repetitions

    题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...

  2. [Swift]LeetCode466. 统计重复个数 | Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  3. 466. Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  4. [LeetCode] Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  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. 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 ...

  7. 【leetcode 字符串】466. Count The Repetitions

    https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...

  8. 第七周 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个 ...

  9. [LeetCode] 466. Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  10. LeetCode466. Count The Repetitions

    题目链接 传送门 题意 定义一个特殊的串, 现在给出串S1和S2的参数, 问: S2最多可以多少个连接起来扔是S1的子序列, 求出这个最大值 解题思路 注意s1与S1的区别, 可以去看题目描述, 预处 ...

随机推荐

  1. js正则匹配

    var account = $('input[name="account"').val(); var re = /^[0-9]+.?[0-9]*$/; if (!re.test(a ...

  2. 多校5-MZL's Border 分类: 比赛 2015-08-05 21:28 7人阅读 评论(0) 收藏

    MZL's Border Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...

  3. opencv的实用研究--分析轮廓并寻找边界点

    opencv的实用研究--分析轮廓并寻找边界点 ​      轮廓是图像处理中非常常见的.对现实中的图像进行采样.色彩变化.灰度变化之后,能够处理得到的是“轮廓”.它直接地反应你了需要分析对象的边界特 ...

  4. C#中容易被忽视的细节整理

    (有空更新系列) 1.params可变长度参数,默认值是长度为0的数组,而不是空 2.事件和委托默认值都是null 3.bool返回值的事件调用之后,其内部的合并方式是取最后一个合并对象的返回值

  5. Unity Shader中自定义枚举类型

    效果 脚本: Properties { _MainTex ("Texture", 2D) = "white" {} [Enum(Enum1,,Enum2,)]_ ...

  6. Linux内存模型

    http://blog.csdn.net/sunyubo458/article/details/6090946 了解linux的内存模型,或许不能让你大幅度提高编程能力,但是作为一个基本知识点应该熟悉 ...

  7. Linux/Unix里,ln -s

    这是硬/软链接的命令ln -s 是创建软链接ln 是创建硬链接 你可以理解为:相当于windows下创建快捷方式一样,所以就不用太多解释了吧. ln -s /usr/lib/libX11.3 libX ...

  8. 如何查看tensorflow版本与存储位置

    import tensorflow as tf tf.__version__ __看着是一个下划线,实际上是两个下划线,中间有空格 tf.__path_

  9. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

  10. Codeforces Round #368 (Div. 2) A

    Description Small, but very brave, mouse Brain was not accepted to summer school of young villains. ...