Jarhyn
Wizard
- Joined
- Mar 29, 2010
- Messages
- 14,819
- Gender
- Androgyne; they/them
- Basic Beliefs
- Natural Philosophy, Game Theoretic Ethicist
I would say your best bet is to jog your string and find the index of every 0. Then map their distances. You aren't doing an exhaustive search on every count, just looking for the high water mark on distance.
You have to read each value once, at a minimum. Why not just read each value .. exactly once?
For int pos = 0; pos < strlen; pos++{
if c == 0, {
diff = cur-pos
If diff > min && diff > hwm{
Hwm = diff
Longestpos = pos}
Pos = cur}}
Oh, I'm wrong, you can also skip work by jumping FORWARD by HWM at each 0 and then searching backwards for a breaking 0 from that forward point, possibly skipping HWM search bits
So, "11100011110001" could be searched by "111" found, skip forward at 0 by 3, see a 1, if you don't count back to the 0, restart from the first 0 before the 1 (since it cannot contain enough 1s to generate a 3 string, our HWM), and then count forward by 3 again, until we have either beaten our HWM or stepped forward again from a 0.
I'll post a general solver with short circuit look-ahead after work
You have to read each value once, at a minimum. Why not just read each value .. exactly once?
For int pos = 0; pos < strlen; pos++{
if c == 0, {
diff = cur-pos
If diff > min && diff > hwm{
Hwm = diff
Longestpos = pos}
Pos = cur}}
Oh, I'm wrong, you can also skip work by jumping FORWARD by HWM at each 0 and then searching backwards for a breaking 0 from that forward point, possibly skipping HWM search bits
So, "11100011110001" could be searched by "111" found, skip forward at 0 by 3, see a 1, if you don't count back to the 0, restart from the first 0 before the 1 (since it cannot contain enough 1s to generate a 3 string, our HWM), and then count forward by 3 again, until we have either beaten our HWM or stepped forward again from a 0.
I'll post a general solver with short circuit look-ahead after work