Skip to content

Instantly share code, notes, and snippets.

@agasiev
Created December 3, 2012 23:51
Show Gist options
  • Save agasiev/4199164 to your computer and use it in GitHub Desktop.
Save agasiev/4199164 to your computer and use it in GitHub Desktop.
Trivial implementation of prefix function
std::vector<unsigned> trivial_prefix_function(std::vector<unsigned> & prefix, std::string s) {
prefix.resize(s.length());
for (int i = 0; i < s.length(); ++i)
for (int j = 0; j <= i; ++j)
if (s.substr(0, j) == s.substr(i - j + 1, j))
prefix[i] = j;
return prefix;
}
int main(int argc, char ** argv) {
std::string str;
std::vector<unsigned> res;
std::cin >> str;
trivial_prefix_function(res, str);
for_each(res.begin(), res.end(), [&](unsigned x) {
cout << x << " ";
});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment