-
Notifications
You must be signed in to change notification settings - Fork 3
/
1764.cpp
53 lines (52 loc) · 1.76 KB
/
1764.cpp
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
class Solution {
public:
vector<int> getSuffix(vector<int>& group) {
int n = group.size();
vector<int> suffix(n, 0);
for (int i = 1; i < n; ++i) {
int j = suffix[i - 1];
while (j >= 1 && group[i] != group[j]) {
j = suffix[j - 1];
}
suffix[i] = j + (group[i] == group[j]);
}
return suffix;
}
bool canChoose(vector<vector<int>>& groups, vector<int>& nums) {
int groupIdx = 0;
int numsIdx = 0;
int n = nums.size();
vector<int> dp(n, 0);
while (groupIdx < groups.size()) {
bool flag = false;
vector<int> suffix = getSuffix(groups[groupIdx]);
// from nums[numsIdx] to match suffix
int start = numsIdx;
dp[numsIdx] = (groups[groupIdx][0] == nums[numsIdx]);
if (dp[numsIdx] == groups[groupIdx].size()) {
groupIdx++;
numsIdx++;
flag = true;
}
if (!flag) {
numsIdx++;
for (; numsIdx < n; ++numsIdx) {
int j = dp[numsIdx - 1];
while (j > 0 && groups[groupIdx][j] != nums[numsIdx]) {
j = suffix[j - 1];
}
dp[numsIdx] = j + (groups[groupIdx][j] == nums[numsIdx]);
if (dp[numsIdx] == groups[groupIdx].size()) {
groupIdx++;
numsIdx++;
flag = true;
break;
}
}
}
if (!flag) return false;
if (numsIdx == n && groupIdx != groups.size()) return false;
}
return true;
}
};