-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 统计重复个数 | ||
// https://leetcode.cn/problems/count-the-repetitions | ||
// INLINE ../../images/string/count_the_repetitions.jpeg | ||
|
||
#include <headers.hpp> | ||
|
||
class Solution { | ||
public: | ||
int getMaxRepetitions(string s1, int n1, string s2, int n2) { | ||
int len1 = s1.length(); | ||
int len2 = s2.length(); | ||
int index1 = 0; | ||
int index2 = 0; | ||
|
||
if (len1 == 0 || len2 == 0 || len1 * n1 < len2 * n2) { | ||
return 0; | ||
} | ||
|
||
unordered_map<int, int> map1; | ||
unordered_map<int, int> map2; | ||
int ans = 0; | ||
|
||
while (index1 / len1 < n1) { | ||
if (index1 % len1 == len1 - 1) { | ||
if (map1.count(index2 % len2)) { | ||
int cycleLen = index1 / len1 - map1[index2 % len2] / len1; | ||
int cycleNum = (n1 - 1 - index1 / len1) / cycleLen; | ||
int cycleS2Num = index2 / len2 - map2[index2 % len2] / len2; | ||
|
||
index1 += cycleNum * cycleLen * len1; | ||
ans += cycleNum * cycleS2Num; | ||
} else { | ||
map1[index2 % len2] = index1; | ||
map2[index2 % len2] = index2; | ||
} | ||
} | ||
|
||
if (s1[index1 % len1] == s2[index2 % len2]) { | ||
if (index2 % len2 == len2 - 1) { | ||
ans += 1; | ||
} | ||
index2 += 1; | ||
} | ||
index1 += 1; | ||
} | ||
return ans / n2; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2024-01-01 11:39:56 | ||
// 执行编译时间:2024-01-02 09:20:33 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <string/count_the_repetitions.cpp> | ||
|
||
TEST(统计重复个数, getMaxRepetitions) { | ||
Solution solution; | ||
// 示例 1: | ||
// 输入:s1 = "acb", n1 = 4, s2 = "ab", n2 = 2 | ||
// 输出:2 | ||
string s1 = "acb"; | ||
int n1 = 4; | ||
string s2 = "ab"; | ||
int n2 = 2; | ||
EXPECT_EQ(solution.getMaxRepetitions(s1, n1, s2, n2), 2); | ||
|
||
// 示例 2: | ||
// 输入:s1 = "acb", n1 = 1, s2 = "acb", n2 = 1 | ||
// 输出:1 | ||
s1 = "acb"; | ||
n1 = 1; | ||
s2 = "acb"; | ||
n2 = 1; | ||
EXPECT_EQ(solution.getMaxRepetitions(s1, n1, s2, n2), 1); | ||
} |