#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main() {
string sample = "aabaa";
string text = "aabaabaaaabaabaaab";
vector<size_t> prefix(sample.length());
size_t last_prefix = prefix[0] = 0;
for (size_t i=1; i<sample.length(); i++) {
while (last_prefix > 0 && sample[last_prefix] != sample[i])
last_prefix = prefix[last_prefix-1];
if (sample[last_prefix] == sample[i])
last_prefix++;
prefix[i] = last_prefix;
}
last_prefix = 0;
for (size_t i=0; i<text.length(); i++) {
while (last_prefix > 0 && sample[last_prefix] != text[i])
last_prefix = prefix[last_prefix-1];
if (sample[last_prefix] == text[i])
last_prefix++;
if (last_prefix == sample.length()) {
cout << i + 1 - sample.length() << endl;
}
}
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkgewoJc3RyaW5nIHNhbXBsZSA9ICJhYWJhYSI7CglzdHJpbmcgdGV4dCA9ICJhYWJhYWJhYWFhYmFhYmFhYWIiOwoJdmVjdG9yPHNpemVfdD4gcHJlZml4KHNhbXBsZS5sZW5ndGgoKSk7CgoJc2l6ZV90IGxhc3RfcHJlZml4ID0gcHJlZml4WzBdID0gMDsKCWZvciAoc2l6ZV90IGk9MTsgaTxzYW1wbGUubGVuZ3RoKCk7IGkrKykgewoJCXdoaWxlIChsYXN0X3ByZWZpeCA+IDAgJiYgc2FtcGxlW2xhc3RfcHJlZml4XSAhPSBzYW1wbGVbaV0pCgkJCWxhc3RfcHJlZml4ID0gcHJlZml4W2xhc3RfcHJlZml4LTFdOwoJCQoJCWlmIChzYW1wbGVbbGFzdF9wcmVmaXhdID09IHNhbXBsZVtpXSkKCQkJbGFzdF9wcmVmaXgrKzsKCQkKCQlwcmVmaXhbaV0gPSBsYXN0X3ByZWZpeDsKCX0KCQoJbGFzdF9wcmVmaXggPSAwOwoJZm9yIChzaXplX3QgaT0wOyBpPHRleHQubGVuZ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