Try here.

> I did indeed get the same output as you for “cat”.

Hmm, how are you testing it? When I test the regex in comment 1124 with print “$i\n” if $i =~ /…/, it seems to match correctly.

> (which adheres to what I wrote in the problem specification, but not exactly to its spirit)

Yes, if I had realized my solution was n^d instead of incorrectly thinking it was n^3, I wouldn’t have implemented it. I might have tried to use | to make a conjunction with de Morgan’s laws. I doubt I would have thought of using the zero-width lookahead assertion to make a conjunction, I’ll have to remember that trick.

]]>Your new link doesn’t link to anything; are you sure you typed it in correctly?

]]>Let’s see, my (crummy) solution will output one term for each way of selecting letters from the word. If there are a_i of letter i, that gives PRODUCT[a_i+1]. For d distinct letters, that’ll max out with a_i = n/d. Which gives (n/d)^d terms, of size d. Which is not the way my code is behaving, so it’s buggy.

And indeed, the way I recurse through the space is simply broken. A fixed version is here. This gives smaller output (academic since we already know this approach is not good): 50k for “restrain”, 102k for “restrains”, 337k for “restrained”, 1.8M for “restraining”. And does seem to be growing with an exponent of d.

]]>Hmm, there must be some undefined behavior or something I accidentally put in my solution, even with the lot-worse-than-I-expected performance, “restrain” only produces 0.6 M for me.

]]>^(?&endc1a1t1)(?(DEFINE)(?<endc1>c$)(?<enda1>a$)(?<endt1>t$)(?<enda1t1>a(?&endt1)|t(?&enda1))(?<endc1t1>c(?&endt1)|t(?&endc1))(?<endc1a1>c(?&enda1)|a(?&endc1))(?<endc1a1t1>c(?&enda1t1)|a(?&endc1t1)|t(?&endc1a1))) ]]>

It compiles cleanly with -Wall, unless you rename it to a C++ file and compile it as such. I’m surprised you don’t know this difference between C and C++. :)

For “cat”, I should produce the PCRE ^(?&endc1a1t1)(?(DEFINE)(?c$)(?a$)(?t$)(?a(?&endt1)|t(?&enda1))(?c(?&endt1)|t(?&endc1))(?c(?&enda1)|a(?&endc1))(?c(?&enda1t1)|a(?&endc1t1)|t(?&endc1a1)))

[Hope those angle brackets and ampersands come through, no “preview” button, unfortunately.]

Is this what you get? Testing this with print “$i\n” if $i =~ /…/ in perl seems to work…

I though my solution would be cubic: It calls printreelems n times, each of which prints n terms, each of size n. But I erred in my term count, it’s the number of ways to pick out i letters from the letters in the word, which has an exponent of # distinct letters. So I can get a shred of “polynomial time” with O(n^26) from the English letters stipulation, but not what I intended, if I had analyzed it correctly I would have not pursued this technique and tried to think of another.

Your solution is much more elegant. I like that way of counting characters.

]]>`void*`

on line 52. After I fixed that, I couldn’t get the output regex to work for small words like “cat”, and I’m pretty sure the size of your regex is exponential in the size of the input (your code gave several megabytes of output for the “restrain” example).
Here’s my solution; it’s 2 lines of Python. Given an input string of length n, the length of its output is O(log n).

I’m TAing the freshman “Intro to CS” course at my school. We’re currently discussing regex, which is how this came up.

]]>I figured counting the characters in an interpolated perl code block would be cheating.

What class are you TAing?

]]>