/*
* @lc app=leetcode id=936 lang=cpp
*
* [936] Stamping The Sequence
*/
// @lc code=start
class Solution {
public:
vector<int> movesToStamp(string stamp, string target) {
const int N = target.length(), M = stamp.length();
vector<int> res;
vector<bool> visited(N);
int total = 0;
const auto un_stamp = [&](int s) {
int l = M;
for (int i = 0; i < M; ++i) {
if (target[s + i] == '*') --l;
else if (target[s + i] != stamp[i]) return 0;
}
if (l != 0) fill(begin(target) + s, begin(target) + s + M, '*');
return l;
};
while (total < N) {
bool found = false;
for (int i = 0; i <= N - M; ++i) {
if (visited[i]) continue;
int l = un_stamp(i);
if (l == 0) continue;
visited[i] = true;
total += l;
res.push_back(i);
found = true;
}
if (!found) return {};
}
reverse(begin(res), end(res));
return res;
}
};
// @lc code=end