# Definition for an infinite stream.# class InfiniteStream:# def next(self) -> int:# passclassSolution:deffindPattern(self,stream:Optional["InfiniteStream"],pattern:List[int])->int:a=b=0m=len(pattern)half=m>>1mask1=(1<<half)-1mask2=(1<<(m-half))-1foriinrange(half):a|=pattern[i]<<(half-1-i)foriinrange(half,m):b|=pattern[i]<<(m-1-i)x=y=0foriincount(1):v=stream.next()y=y<<1|vv=y>>(m-half)&1y&=mask2x=x<<1|vx&=mask1ifi>=manda==xandb==y:returni-m
/** * Definition for an infinite stream. * class InfiniteStream { * public InfiniteStream(int[] bits); * public int next(); * } */classSolution{publicintfindPattern(InfiniteStreaminfiniteStream,int[]pattern){longa=0,b=0;intm=pattern.length;inthalf=m>>1;longmask1=(1L<<half)-1;longmask2=(1L<<(m-half))-1;for(inti=0;i<half;++i){a|=(long)pattern[i]<<(half-1-i);}for(inti=half;i<m;++i){b|=(long)pattern[i]<<(m-1-i);}longx=0,y=0;for(inti=1;;++i){intv=infiniteStream.next();y=y<<1|v;v=(int)((y>>(m-half))&1);y&=mask2;x=x<<1|v;x&=mask1;if(i>=m&&a==x&&b==y){returni-m;}}}}
/** * Definition for an infinite stream. * type InfiniteStream interface { * Next() int * } */funcfindPattern(streamInfiniteStream,pattern[]int)int{a,b:=0,0m:=len(pattern)half:=m>>1mask1:=(1<<half)-1mask2:=(1<<(m-half))-1fori:=0;i<half;i++{a|=pattern[i]<<(half-1-i)}fori:=half;i<m;i++{b|=pattern[i]<<(m-1-i)}x,y:=0,0fori:=1;;i++{v:=stream.Next()y=y<<1|vv=(y>>(m-half))&1y&=mask2x=x<<1|vx&=mask1ifi>=m&&a==x&&b==y{returni-m}}}