Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
stamping-the-sequence.cpp 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-11-04 21:18 +08:00 . Create stamping-the-sequence.cpp
// Time: O((n - m) * m)
// Space: O((n - m) * m)
class Solution {
public:
vector<int> movesToStamp(string stamp, string target) {
const int M = stamp.size(), N = target.size();
queue<int> q;
vector<bool> lookup(N);
vector<int> result;
vector<pair<unordered_set<int>, unordered_set<int>>> A;
for (int i = 0; i < N - M + 1; ++i) {
unordered_set<int> made, todo;
for (int j = 0; j < M; ++j) {
if (stamp[j] == target[i + j]) {
made.emplace(i + j);
} else {
todo.emplace(i + j);
}
}
A.emplace_back(made, todo);
if (!todo.empty()) {
continue;
}
result.emplace_back(i);
for (const auto& m : made) {
if (lookup[m]) {
continue;
}
q.emplace(m);
lookup[m] = true;
}
}
while (!q.empty()) {
auto i = q.front(); q.pop();
for (int j = max(0, i - M + 1); j < min(N - M, i) + 1; ++j) {
unordered_set<int>& made = A[j].first;
unordered_set<int>& todo = A[j].second;
if (!todo.count(i)) {
continue;
}
todo.erase(i);
if (!todo.empty()) {
continue;
}
result.emplace_back(j);
for (const auto& m : made) {
if (lookup[m]) {
continue;
}
q.emplace(m);
lookup[m] = true;
}
}
}
if (std::all_of(lookup.cbegin(), lookup.cend(), [](bool i){ return i; })) {
reverse(result.begin(), result.end());
return result;
}
return {};
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助