Prefix- and z-functions in C++ (string algorithms) Let us define prefix-function(string)[i] as the length of the largest prefix of string, which is also a suffix of string[1..i], and z-function(string)[i] is the length of the largest prefix of string, which is also a prefix of string[i..].

Unfortunately, there is very little information on them in English, but I found a presentation on Knuth-Morris-Pratt algorithm containing description of prefix-function and an article on z-function.

Here is my implementation of prefix-function and z-function and conversions between them in linear time on the length of string using pure C++. Comments are appreciated! In particular, I'm interesting how to determine a string from z-function directly.

std::vector&lt;int&gt; prefix_function (const std::string&amp; str) {
    std::vector&lt;int&gt; prefs (str.size(), 0);
    for (int i = 1; i &lt; str.size(); ++i) {
        int pref = prefs[i-1];
        while (pref &gt; 0 &amp;&amp; str[i] != str[pref]) {
            pref = prefs[pref-1];
        }
        if (str[i] == str[pref]) {
            ++pref;
        }
        prefs[i] = pref;
    }
    return prefs;
}

std::vector&lt;int&gt; z_function(const std::string&amp; str) {
    std::vector&lt;int&gt; zfunc (str.size(), 0);
    zfunc[0] = str.size();
    for (int i = 1, left = 0, right = 0; i &lt; str.size(); ++i) {
        if (i &lt;= right) {
            zfunc[i] = std::min (right - i + 1, zfunc[i - left]);
        }
        while (i + zfunc[i] &lt; str.size() &amp;&amp; str[zfunc[i]] == str[i + zfunc[i]]) {
            ++zfunc[i];
        }
        if (i + zfunc[i] - 1 &gt; right) {
            left = i;
            right = i + zfunc[i] - 1;
        }
    }
    return zfunc;
}

std::string from_prefix_function(const std::vector&lt;int&gt;&amp; prefs) {
    std::string str (prefs.size(), '.');
    char current_symbol = 'a';
    for (int i = 0; i &lt; prefs.size(); ++i) {
        if (prefs[i] &gt; 0) {
            str[i] = str[prefs[i] - 1];
        } else {
            str[i] = current_symbol++;
        }
    }
    return str;
}

std::vector&lt;int&gt; prefix_to_z(const std::vector&lt;int&gt;&amp; prefs) {
    return z_function(from_prefix_function(prefs));
}

std::vector&lt;int&gt; z_to_prefix(const std::vector&lt;int&gt;&amp; z_func) {
    std::vector&lt;int&gt; prefs (z_func.size(), 0);
    for (int i = 1; i &lt; z_func.size(); ++i) {
        prefs[i + z_func[i] - 1] = std::max(prefs[i + z_func[i] - 1], z_func[i]);
    }
    for (int i = z_func.size() - 2; i &gt;= 0; --i) {
        prefs[i] = std::max(prefs[i+1] - 1, prefs[i]);
    }
    return prefs;
}
This will be a somewhat strange answer but the thing is that I can't find anything wrong with your code: it is not that easy to understand what it is doing, but it is only because the algorithms are not the most straightforward in the world; if we compare your code and the algorithms pseudo-code, it's easy to see that they are performing the same computations. Everything seems well-written, the language is used as it should be used... In the end, I only have a few almost-irrelevant remarks left:


The only obvious thing you could do is to templatize your algorithm so that it works for any std::basic_string specialization and not only for std::string.
If you strive for speed and can overcome a little less readability (generally, you don't), you could try to change this piece of code:

if (str[i] == str[pref]) {
    ++pref;
}


into this one:

pref += str[i] == str[pref];


But even then, time it, time it again, and I'm pretty sure that the compiler is somehow smart enough to get rid of that branch for you and that it won't impact the overall speed of the algorithm in any significant way.
You are slightly inconsistent in the way you call the functions: you wrote std::min (/* ... */) but also std::max(/* ... */). You should either put a space everywhere or remove it everywhere, but be consistent.


Well, as you see, there is little to say about what you wrote. It's some pretty good code. Great job writing it! :)