Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <tuple>
- using namespace std;
- vector<int> zfunc(const string& s){
- vector<int> z(s.size());
- int first = 0;
- int last = 1;
- for (int i = 1; i < s.size(); ++i){
- if (i >= first && i < last && z[i - first] + i < last){
- z[i] = z[i - first];
- } else {
- z[i] = max(0, last - i);
- first = i;
- while (s[i + z[i]] == s[z[i]] && i + z[i] < s.size()){
- ++z[i];
- }
- last = i + z[i];
- }
- }
- return z;
- }
- int main() {
- string s1, s2;
- cin >> s1 >> s2;
- string s = s2 + "#" + s1;
- vector<int> z = zfunc(s);
- for (size_t i = s2.size() + 1; i < s.size(); ++i){
- if (z[i] == s2.size()){
- cout << i - s2.size() - 1 << " ";
- }
- }
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement