-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
65 lines (64 loc) · 1.75 KB
/
solution.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
typedef pair<int, int> pii;
class Solution {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
int k = 0;
int num = 0;
int idx = 0;
vector<pii> V;
V.push_back({num, idx});
unordered_map<int, int> M;
M[0] = 0;
auto work = [&](int &num, int &idx) {
int sz = s1.size();
for (int i = 0; i < sz; i++) {
if (s1[i] == s2[idx]) {
idx = (idx + 1) % s2.size();
if (idx == 0) {
num++;
}
}
}
};
int cycle_status;
while (true) {
k++;
work(num, idx);
if (M.count(idx)) {
cycle_status = idx;
break;
}
M[idx] = k;
V.push_back({num, idx});
}
// k 轮 有 num 个 s2 no
debug(k, num, V, cycle_status);
if (n1 < k) {
int have = V[n1].first;
return have / n2;
} else {
int t = M[cycle_status];
int have = V[t].first;
int cycle = k - t;
int one_num = num - have;
n1 -= t;
int times = n1 / cycle * one_num;
times += have;
debug(times);
int times_have = n1 % cycle;
auto it = V[t + times_have];
times += it.first - V[t].first;
return times / n2;
}
}
};