OFFSET
1,2
REFERENCES
Knuth-Morris-Pratt pattern matching algorithm.
LINKS
Dennis Moore, W. F. Smyth and Dianne Miller, Counting distinct strings, Algorithmica, Vol. 23 (1999), 1-13.
EXAMPLE
For example, a string of length 3 can have one of the following 4 'failure tables': 012, 001, 010, 000.
PROG
(C++)
// check(p, n) returns true if and only if there exists a string of length n that have provided failure table (assuming that p[0] == -1).
bool check(int *p, int n) {
static int a[64];
for (int i = 0; i <= n; i++)
a[i] = i;
for (int i = 1, k = 0; i <= n; i++) {
for (; k >= p[i]; k = p[k]);
if (++k != p[i])
return false;
if (k)
a[i] = a[k];
}
for (int i = 1, k = 0; i <= n; i++, k++)
for (; k >= p[i]; k = p[k])
if (k + 1 < i && a[k + 1] == a[i])
return false;
return true;
}
// count(n) returns number of different failure tables for string of length n.
long long count(int n, int i = 1) {
static int p[64] = {-1};
if (!check(p, i - 1))
return 0;
if (i > n)
return 1;
long long result = 0;
for (p[i] = 0; p[i] <= p[i - 1] + 1; p[i]++)
result += count(n, i + 1);
return result;
} // Pavel Irzhavski, Feb 25 2014
CROSSREFS
KEYWORD
nonn,nice
AUTHOR
Dianne Miller (millerdm(AT)mcmaster.ca)
EXTENSIONS
a(19) and beyond from Pavel Irzhavski, Feb 25 2014
STATUS
approved