Your guess is definitely on the right track, though you may need one more piece of information. As you have infinite states (of which each input will use some number of them according to a function of the length of the input), you can use (as you observe) the states to encode information that deals with the length of the input.
There are a number of ways to do this (you can use the same variables as the code essentially), but I think it's clearer to be more explicit. Each state can keep track of the following six things:
- The starting position of the best run so far.
- The ending position of the best run so far.
- The length of the best run so far.
- The starting position of the current run being counted.
- The ending position of the current run being counted.
- The length of the current run being counted.
So each state could be labeled something like:
$$q_{maxstart,maxend,maxlength,currentstart,currentend,currentlength}$$
This then solves the transition problem:
- A zero in the input ends the current run, and resets to start counting again, but doesn't affect the best run so far
- A one in the input increases the end point of the current run (and sets the starting point if the current run length is zero) AND if you happen to be in a state where the current run length is the same as the best run length, it also alters the best run length subscripts (that is, the transition takes you to a state where those change suitably).
There's no branching in either of those, so the process is completely deterministic.
A couple of examples to illustrate:
- The start state is easy, it's just $q_{0,0,0,0,0,0}$ with the transitions $\delta(q_{0,0,0,0,0,0},0) = q_{0,0,0,1,1,0}$ and $\delta(q_{0,0,0,0,0,0},1) = q_{0,1,1,0,1,1}$. This uses the case where the current run length is the same as the best, and we see a one, so we update the best start and end to be the same as the current start and end etc.
- Now let's try a state where the current run changes, but isn't better than the best. Imagine we're in state $q_{a,b,50,x,y,3}$, then the two transitions will be $\delta(q_{a,b,50,x,y,3},0) = q_{a,b,50,y+1,y+1,0}$ and $\delta(q_{a,b,50,x,y,3},1) = q_{a,b,50,x,y+1,4}$.
This actually pretty much covers all the cases (as there's really not much going on except the abuse of subscripts to count for us).
To boil that down to some rules for putting in the transitions:
- $\delta(q_{a,b,n,x,y,m},0) = q_{a,b,n,y+1,y+1,0}$
- If $m < n$, $\delta(q_{a,b,n,x,y,m},1) = q_{a,b,n,x,y+1,m+1}$
- If $m = n$, $\delta(q_{a,b,n,x,y,n},1) = q_{x,y+1,n+1,x,y+1,n+1}$
You don't need any states where $m > n$, so you don't have to worry about that case (though you can include them and give them a rule like #3 if you want a complete infinite enumeration of all the subscripts.