-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3037-FindPatternInInfiniteStreamII.go
More file actions
141 lines (126 loc) · 4.43 KB
/
3037-FindPatternInInfiniteStreamII.go
File metadata and controls
141 lines (126 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
// 3037. Find Pattern in Infinite Stream II
// You are given a binary array pattern and an object stream of class InfiniteStream representing a 0-indexed infinite stream of bits.
// The class InfiniteStream contains the following function:
// int next(): Reads a single bit (which is either 0 or 1) from the stream and returns it.
// Return the first starting index where the pattern matches the bits read from the stream.
// For example, if the pattern is [1, 0], the first match is the highlighted part in the stream [0, 1, 0, 1, ...].
// Example 1:
// Input: stream = [1,1,1,0,1,1,1,...], pattern = [0,1]
// Output: 3
// Explanation: The first occurrence of the pattern [0,1] is highlighted in the stream [1,1,1,0,1,...], which starts at index 3.
// Example 2:
// Input: stream = [0,0,0,0,...], pattern = [0]
// Output: 0
// Explanation: The first occurrence of the pattern [0] is highlighted in the stream [0,...], which starts at index 0.
// Example 3:
// Input: stream = [1,0,1,1,0,1,1,0,1,...], pattern = [1,1,0,1]
// Output: 2
// Explanation: The first occurrence of the pattern [1,1,0,1] is highlighted in the stream [1,0,1,1,0,1,...], which starts at index 2.
// Constraints:
// 1 <= pattern.length <= 10^4
// pattern consists only of 0 and 1.
// stream consists only of 0 and 1.
// The input is generated such that the pattern's start index exists in the first 105 bits of the stream.
import "fmt"
import "strconv"
type InfiniteStream struct {
data []int
index int
}
func (this *InfiniteStream) Next() int {
this.index++
if this.index == len(this.data) {
this.index = 0
}
return this.data[this.index]
}
func Constructor(data []int) InfiniteStream {
return InfiniteStream{ data, 0 }
}
/**
* Definition for an infinite stream.
* type InfiniteStream interface {
* Next() int
* }
*/
/**
* Definition for an infinite stream.
* type InfiniteStream interface {
* Next() int
* }
*/
func findPattern(stream InfiniteStream, pattern []int) int {
pStr, nowStr, index := "", "", 0
for i := 0; i < len(pattern); i++ {
pStr += strconv.Itoa(pattern[i])
}
for {
s := strconv.Itoa(stream.Next())
nowStr += s
if len(nowStr) == len(pStr) {
if pStr == nowStr {
return index - len(pattern) + 1
}
nowStr = nowStr[1:]
}
index++
}
return -1
}
// kmp 模板题
func findPattern1(stream InfiniteStream, pattern []int) int {
n := len(pattern)
pi := make([]int, n) // 计算pi数组
count := 0
for i := 1; i < n; i++ {
for count > 0 && pattern[i] != pattern[count] {
count = pi[count-1]
}
if pattern[i] == pattern[count] {
count++
pi[i] = count
}
}
// kmp
count = 0
for i := 0; ; i++ {
ch := stream.Next()
for count > 0 && pattern[count] != ch {
count = pi[count - 1]
}
if pattern[count] == ch {
count++
if count == n {
return i - n + 1 // [i-n+1:i]共n个字符
}
}
}
return -1
}
func main() {
// Example 1:
// Input: stream = [1,1,1,0,1,1,1,...], pattern = [0,1]
// Output: 3
// Explanation: The first occurrence of the pattern [0,1] is highlighted in the stream [1,1,1,0,1,...], which starts at index 3.
stream1 := Constructor([]int{1,1,1,0,1,1,1,0})
fmt.Println(findPattern(stream1, []int{0,1})) // 3
// Example 2:
// Input: stream = [0,0,0,0,...], pattern = [0]
// Output: 0
// Explanation: The first occurrence of the pattern [0] is highlighted in the stream [0,...], which starts at index 0.
stream2 := Constructor([]int{0,0,0,0})
fmt.Println(findPattern(stream2, []int{0})) // 0
// Example 3:
// Input: stream = [1,0,1,1,0,1,1,0,1,...], pattern = [1,1,0,1]
// Output: 2
// Explanation: The first occurrence of the pattern [1,1,0,1] is highlighted in the stream [1,0,1,1,0,1,...], which starts at index 2.
stream3 := Constructor([]int{1,0,1,1,0,1,1,0,1})
fmt.Println(findPattern(stream3, []int{1,1,0,1})) // 2
stream11 := Constructor([]int{1,1,1,0,1,1,1,0})
fmt.Println(findPattern1(stream11, []int{0,1})) // 3
stream12 := Constructor([]int{0,0,0,0})
fmt.Println(findPattern1(stream12, []int{0})) // 0
stream13 := Constructor([]int{1,0,1,1,0,1,1,0,1})
fmt.Println(findPattern1(stream13, []int{1,1,0,1})) // 2
}