Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- void prefix_func(std::string s, int n, std::vector<int>& p)
- {
- for (int i = 1, j; i < n; ++i)
- {
- j = p[i - 1];
- while (j > 0 && s[i] != s[j])
- j = p[j - 1];
- if (s[i] == s[j])
- ++j;
- p[i] = j;
- }
- }
- int main()
- {
- std::string s;
- std::cin >> s;
- int n = s.length();
- std::vector<int> p(n, 0);
- prefix_func(s, n, p);
- for (int i = 0; i < n; ++i)
- std::cout << p[i] << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment