Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- std::vector<int> Prefix(std::string str){
- std::vector<int> res;
- res.push_back(0);
- for (int i = 1; i < str.size()-1 ; ++i) {
- int k = res[i - 1];
- while (k > 0 && str[i] != str[k])
- k = res[k - 1];
- if (str[i] == str[k]) {
- k++;
- }
- res.push_back(k);
- }
- return res;
- }
- int main() {
- std::string a;
- cin>>a;
- std::vector<int> res;
- res = Prefix(a);
- for (int i = 0; i<res.size(); ++i) {
- cout<<res[i]<<" ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement