-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
132 lines (115 loc) · 2.02 KB
/
run.go
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
package substring
import (
"fmt"
"strings"
)
// 超时
func longestPalindrome(s string) string {
l := len(s)
if l < 2 {
return s
}
// m, j := "", l-1
// for j > 0 {
// for i := 0; i < l; i++ {
// fmt.Println(i, ":", j)
// if i < j && s[i] == s[j] && len(s[i:j+1]) > len(m) {
// fmt.Println(i, ":", j)
// fmt.Println(string(s[i]), "-", string(s[j]))
// fmt.Println(string(s[i:j+1]), "~", m)
// if isPalindrome(string(s[i : j+1])) {
// m = s[i : j+1]
// }
// fmt.Println("m:", m)
// }
// }
// j--
// }
m, j := "", 0
for j < l-1 {
for i := l - 1; i > 0; i-- {
if i > j && s[i] == s[j] && len(s[j:i+1]) > len(m) {
max := string(s[j : i+1])
if isPalindrome(max) {
m = max
}
fmt.Println("m:", m)
}
}
j++
}
if m == "" {
m = string(s[j])
}
return m
}
// 判断是否为回文串
func isPalindrome(s string) (b bool) {
fmt.Println("p:", s)
l, j := len(s), len(s)-1
for i := 0; i < l/2; i++ {
if s[i] == s[j] {
b = true
} else {
b = false
break
}
j--
}
fmt.Println(b)
return
}
func handleString(s string) string {
l := len(s)
if l == 0 {
return "^$"
}
r := make([]string, 0, l)
for _, v := range s {
r = append(r, string(v))
}
return fmt.Sprintf("^#%s#$", strings.Join(r, "#"))
}
// Manacher
func manacher(s string) string {
if len(s) == 0 {
return ""
}
str := handleString(s)
ls := len(str)
p := make([]int, ls)
c, r := 0, 0
for i := 1; i < ls-1; i++ {
if r > i {
i2 := 2*c - i
if p[i2] < r-i {
p[i] = p[i2]
} else {
p[i] = p[r-i]
}
} else {
p[i] = 0
}
for string(str[i+1+p[i]]) == string(str[i-1-p[i]]) {
p[i]++
}
if i+p[i] > r {
c = i
r = i + p[i]
}
}
maxlen, centerIndex := 0, 0
for i := 1; i < ls-1; i++ {
if p[i] > maxlen {
maxlen = p[i]
centerIndex = i
}
}
fmt.Println(centerIndex, ":", maxlen)
fmt.Println("s:", s)
start := (centerIndex - 1 - maxlen) / 2
end := start + maxlen
fmt.Println("start:", start)
fmt.Println("end:", end)
return s[start:end]
}