Розбір
Сompute the -function for the string and store its values in the array . Print the -function values for all positions .
Algorithm realization
The input string contains no more than characters. The -function of the string is stored in the vector .
vector<int> v; string s;
The z function computes and returns the -function for the string .
vector<int> z(string s) { int i, l, r, len = s.size(); vector<int> z(len, 0); l = 0, r = 0; for (i = 1; i < len; i++) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < len && s[z[i]] == s[i + z[i]]) z[i]++; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } return z; }
The main part of the program. Read the input string .
getline(cin,s);
Compute the -function.
v = z(s);
Print the -function values for all positions of the input string.
for (i = 0; i < v.size(); i++) printf("%d ", v[i]); printf("\n");