-
Notifications
You must be signed in to change notification settings - Fork 17
/
palindrome-concatenation.go
237 lines (178 loc) · 4.59 KB
/
palindrome-concatenation.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import "fmt"
import "sort"
type Pairs [][]int
func (array Pairs) Len() int {
return len(array)
}
func (array Pairs) Less(i, j int) bool {
if array[i][0] < array[j][0] {
return true
} else if array[i][0] == array[j][0] {
if array[i][1] < array[j][1] {
return true
}
}
return false
}
func (array Pairs) Swap(i, j int) {
array[i], array[j] = array[j], array[i]
}
type TrieNode struct {
children map[rune]*TrieNode
end bool
word int
}
func manacher(word string, leftOffset int) []bool {
left, right := 0, -1
cache := make([]int, len(word))
result := make([]bool, len(word)+1)
for pos, _ := range result {
result[pos] = false
}
for pos, _ := range word {
radius := 0
if pos > right {
radius = 1 - leftOffset
} else {
if cache[left+right-(pos-leftOffset)]+1 < right-pos+1 {
radius = cache[left+right-(pos-leftOffset)] + 1
} else {
radius = right - pos + 1
}
}
for 0 <= pos-leftOffset-radius && pos+radius < len(word) && word[pos-leftOffset-radius] == word[pos+radius] {
radius++
}
radius -= 1
cache[pos] = radius
if pos+radius == len(word)-1 {
result[pos-leftOffset-radius] = true
}
if pos+radius > right {
left = pos - leftOffset - radius
right = pos + radius
}
}
return result
}
func isPalindromeFrom(word string) []bool {
notShifted := manacher(word, 0)
shiftedByOne := manacher(word, 1)
result := make([]bool, len(word))
for pos, _ := range word {
result[pos] = notShifted[pos] || shiftedByOne[pos]
}
result = append(result, true)
return result
}
func buildTrie(strings []string) *TrieNode {
trieRoot := &TrieNode{map[rune]*TrieNode{}, false, -1}
for wordPos, word := range strings {
trieNode := trieRoot
for _, char := range word {
if _, ok := trieNode.children[char]; !ok {
trieNode.children[char] = &TrieNode{map[rune]*TrieNode{}, false, -1}
}
trieNode = trieNode.children[char]
}
trieNode.end = true
trieNode.word = wordPos
}
return trieRoot
}
func searchTrie(word string, isPalindrome []bool, trieRoot *TrieNode) []int {
trieNode := trieRoot
result := []int{}
if trieNode.end && isPalindrome[0] {
result = append(result, trieNode.word)
}
for pos, char := range word {
if val, ok := trieNode.children[char]; ok {
trieNode = val
} else {
break
}
if isPalindrome[pos+1] && trieNode.end {
result = append(result, trieNode.word)
}
}
return result
}
func reverseString(str string) string {
result := make([]rune, len(str))
for pos, char := range str {
result[len(str)-pos-1] = char
}
return string(result)
}
func getPairs(strings []string) Pairs {
stringsReversed := []string{}
for _, str := range strings {
stringsReversed = append(stringsReversed, reverseString(str))
}
trieRoot := buildTrie(strings)
trieRootReversed := buildTrie(stringsReversed)
isPalindrome := [][]bool{}
for _, word := range strings {
isPalindrome = append(isPalindrome, isPalindromeFrom(word))
}
isPalindromeReversed := [][]bool{}
for _, word := range stringsReversed {
isPalindromeReversed = append(isPalindromeReversed, isPalindromeFrom(word))
}
results := Pairs{}
for pos, word := range strings {
for _, palindromeWith := range searchTrie(word, isPalindrome[pos], trieRootReversed) {
if pos != palindromeWith {
results = append(results, []int{pos + 1, palindromeWith + 1})
}
}
}
for pos, word := range stringsReversed {
for _, palindromeWith := range searchTrie(word, isPalindromeReversed[pos], trieRoot) {
if pos != palindromeWith {
results = append(results, []int{palindromeWith + 1, pos + 1})
}
}
}
sort.Sort(results)
resultsFiltered := Pairs{}
prevResult := []int{-1, -1}
for _, result := range results {
if !(result[0] == prevResult[0] && result[1] == prevResult[1]) {
resultsFiltered = append(resultsFiltered, result)
prevResult = result
}
}
return resultsFiltered
}
func getNumberOfWords() int {
var numWords int
fmt.Scanf("%d", &numWords)
return numWords
}
func getWord() string {
var word string
fmt.Scanf("%s\n", &word)
return word
}
func main() {
/*
fmt.Println(buildTrie([]string{"bba"}))
fmt.Println(isPalindromeFrom("bbabbbb"))
trieNode := buildTrie([]string{"test", "xxx", "bba", "yyy", "bbab"})
fmt.Println(reverseString("test"))
fmt.Println(searchTrie("bbabbbb", isPalindromeFrom("bbabbbb"), trieNode))
strings := []string{"a", "abbaa", "bba", "abb"}
fmt.Println(getPairs(strings))
*/
numWords := getNumberOfWords()
words := []string{}
for range make([]bool, numWords) {
words = append(words, getWord())
}
for _, pair := range getPairs(words) {
fmt.Println(pair[0], pair[1])
}
}