// Palindrome is a string that is the same forward and backward
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
var str string
if len(scanner.Text()) > 2 {
str = palindrome(scanner.Text())
}
if len(str) > 2 {
fmt.Println(str)
}
}
if scanner.Err() != nil {
fmt.Fprintln(os.Stderr, "Error reading console input")
}
}
// Returns the longest palindrome in str
func palindrome(str string) string {
longest := ""
jobs := 0
results := make(chan string)
for k, _ := range str {
if k > 0 {
go pal_worker(str, k, results)
jobs++
}
}
for {
select {
case candidate := <-results:
jobs--
if len(candidate) > len(longest) {
longest = candidate
}
if jobs == 0 {
close(results)
return longest
}
}
}
}
func pal_worker(str string, k int, result chan<- string) {
longest := string(str[k])
for i := 1; true; i++ {
// avoid overrun or underrun
if k-i < 0 || k+i > len(str)-1 {
break
}
// two identical bytes
if str[k-i] == str[k+i] {
longest = string(str[k-i]) + longest + string(str[k+i])
} else {
break
}
}
//fmt.Fprintf(os.Stderr, "Worker(%d) result: %s\n", k, longest)
result <- longest
}