(This challenge is related to the challenge "Generate the Abacaba sequence.")
Zimin words (also called "sesquipowers") are an important idea in the subject of "combinatorics on words". This is because every Zimin word is "unavoidable", meaning that every sufficiently long word over a finite alphabet has a substring that is an "instance" (defined below) of the Zimin word. Remarkably, every binary word of length 29 has a substring that is an instance of the pattern \$ABACABA\$.
Definitions
Zimin words are recursively defined:
- \$Z_1 = A\$
- \$Z_2 = ABA\$
- \$Z_3 = ABACABA\$
- \$Z_4 = ABACABADABACABA\$
- \$\vdots\$
- \$Z_{n+1} = Z_n X Z_n\$
It's a little tricky to formally define what it means to be an "instance" for a word without talking about free monoids and homomorphisms, but it's easy enough to see from some examples. At a hand-wave-y level, a word \$w\$ is said to be an "instance" of a pattern if there's a way to assign a (non-empty) string to each letter of the pattern and recover \$w\$.
Examples
Consider the word "11211212211221"
on two letters.
- It has \$105\$ substrings that are instances of the Zimin word \$Z_1 = A\$ because every substring is an instance of \$A\$.
- It has \$54\$ substrings that are instances of the Zimin word \$Z_2 = ABA\$—for example, the substring
"212211221"
: $$ 11211\underbrace{21}_A\underbrace{22112}_B\underbrace{21}_A. $$ - It has \$1\$ substring that is an instance of the Zimin word \$Z_3 = ABACABA\$: $$ 11 \underbrace{2}_A \underbrace{11}_B \underbrace{2}_A \underbrace{12}_C \underbrace{2}_A \underbrace{11}_B \underbrace{2}_A 21. $$
- It has \$0\$ substrings that are instances of the Zimin word \$Z_4 = ABACABADABACABA\$.
Thus if you are given "11211212211221"
, you should return [105, 54, 1]
.
Challenge
This code-golf challenge will give you a word as an an input, and asks you to output a list of the number of substrings that are instances \$Z_1, Z_2, \cdots, Z_n\$ until there are not any substrings that are instances. That is, your program should never output 0
.
You can take the word in any reasonable way: as a string, as a list of strings, as a list of appropriately coded integers, as a pair of integers \$(b,i)\$ where \$i\$ is base-\$b\$ encoded integer, etc.
Test Data
word | zimin_counts(word)
-------------------------------+-------------------
"" | []
"P" | [1]
"MOM" | [6,1]
"alfalfa" | [28, 8]
"aha aha!" | [36, 9, 1]
"xxOOOxxxxxOOOxxx" | [136, 64, 9]
"3123213332331112232" | [190, 55, 1]
"2122222111111121221" | [190, 80, 3]
"13121311213121311212" | [210, 114, 25, 2]
"344112222321431121114" | [231, 48]
"141324331112324224341" | [231, 49]
"344342224124222122433" | [231, 61, 1]
"331123321122132312321" | [231, 74]
"11221112211212122222122" | [276, 149, 4]
"2222222112211121112212121" | [325, 166, 6]
"AAAaaAAAAAAAaAaAAaAaAAAAA" | [325, 198, 30]
"1100000010010011011011111100" | [406, 204]
"1111221211122111111221211121" | [406, 261, 56, 2]
_ABACABA_
could be a Z₂ with A =ABA
, B =C
, or it could be a Z₂ with A =A
, B =BACAB
. Does this count once or twice? \$\endgroup\$ABACABA
indeed matches \$Z_2\$ in two different ways, but here we're counting the number of substrings not the number of matches, so it would only be counted once. \$\endgroup\$1
are counter more than once. I'm not sure what a good way to phrase this is but we are really counting sections of the string, which yield strings. I think it would be good to make this clear because without trying to solve the examples on your own and comparing with the presented results it is not possible to tell that you want the same string counted once for each time it appears. \$\endgroup\$