-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtask.go
More file actions
44 lines (37 loc) · 719 Bytes
/
Copy pathtask.go
File metadata and controls
44 lines (37 loc) · 719 Bytes
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
s := strings.Builder{}
Solution(os.Stdin, &s)
fmt.Println(strings.TrimSpace(s.String()))
}
func Solution(r io.Reader, s *strings.Builder) {
scanner := bufio.NewScanner(r)
const bufCapacity = 100000000 // fix long string
buf := make([]byte, bufCapacity)
scanner.Buffer(buf, bufCapacity)
scanner.Scan()
str := scanner.Text()
dp := make([]int, len(str))
s.WriteString("0 ")
for i := 1; i < len(str); i++ {
k := dp[i-1]
for k > 0 && str[i] != str[k] {
k = dp[k-1]
}
if str[i] == str[k] {
dp[i] = k + 1
s.WriteString(strconv.Itoa(dp[i]) + " ")
} else {
dp[i] = 0
s.WriteString("0 ")
}
}
}