問題はこちらのリンクから。
http://qiita.com/Nabetani/items/0b56395d4c9e7c64b230
Rubyで解いてみたが( http://qiita.com/nidouchi/items/3ba43c6021d1e4d54fcd )、処理時間かかりすぎなのでGoに初挑戦。結果、我慢できる時間内で処理できるようになったけれど、アルゴリズムは基本的にRuby版と変えていないので遅いのは変わらず。
package main
import "fmt"
import "strings"
func judge(chars []string) bool {
for i := 0; i < len(chars) / 2; i++ {
if chars[i] != chars[len(chars) - i - 1] {
return false
}
}
return true
}
func countBits(v uint) int {
count := 0
for i := 0; i < 32; i++ {
if ((1<<uint(i)) & v) != 0 {
count++
}
}
return count
}
func solve(prob string) int {
max := 0
orgChars := strings.Split(prob,"")
for i := 1; i < (1 << uint(len(prob))); i++ {
cb := countBits(uint(i))
if cb <= max {
continue
}
selChars := make([]string, cb)
k := 0
for j := 0; j < len(prob); j++ {
if ((1<<uint(j)) & i) != 0 {
selChars[k] = orgChars[j]
k++
}
}
if judge(selChars) {
max = cb
}
}
return max
}
func test(prob string, ans int) {
a := solve(prob)
result := "NG"
if ans == a {
result = "OK"
}
fmt.Printf("%s %d %s\n", prob, a, result)
}
func main() {
/*0*/ test( "I_believe_you_can_solve",9 )
/*1*/ test( "a",1 )
/*2*/ test( "aa",2 )
/*3*/ test( "aaa",3 )
/*4*/ test( "ab",1 )
/*5*/ test( "aabb",2 )
/*6*/ test( "ABBA",4 )
/*7*/ test( "Abba",2 )
/*8*/ test( "1234567890",1 )
/*9*/ test( "1234567890987654321",19 )
/*10*/ test( "abcdcba",7 )
/*11*/ test( "0a1b2c3d4c5b6a7",7 )
/*12*/ test( "abcdcba0123210",7 )
/*13*/ test( "abcdcba_123210",7 )
/*14*/ test( "_bcdcba0123210",7 )
/*15*/ test( "abcddcba0123210",8 )
/*16*/ test( "abcdcba01233210",8 )
/*17*/ test( "a0bc1dc2ba3210a",9 )
/*18*/ test( "a0bc1ddc2ba3210",8 )
/*19*/ test( "3a0bc1ddc2ba3210",10 )
/*20*/ test( "11oooo1111o1oo1o111ooooooooooo",20 )
/*21*/ test( "11o1111o1111oo11ooo11111ooo1oo",20 )
/*22*/ test( "111111oo11o111ooo1o1ooo11ooo1o",21 )
/*23*/ test( "11o1o1o11oo11o11oo111o1o1o11oo",27 )
/*24*/ test( "oo111o1o11o1oo1ooo11o1o11o1o1o",27 )
/*25*/ test( "1o1oo11111o1o1oo1o1o1111oo1o1o",28 )
/*26*/ test( "QQooooQooooQQyQoyQQQyyyyQQoyoy",15 )
/*27*/ test( "oQoooQooooQyoyQoyoyyyQQyQQQQoQ",16 )
/*28*/ test( "yyyyyooyQyyyoyyQyyooyQoQoQQoQy",17 )
/*29*/ test( "yyQoyQoyyQyQQoyooooyyQQyQyooQy",24 )
/*30*/ test( "oQQooQoQyQQoyoQQoQyQyQyQoQoooo",24 )
/*31*/ test( "oQQyQQyyQyQQoooyQQyyyQQQyyQQoy",25 )
/*32*/ test( "WAk9iHI4jVDlStyudwTNqE138kwan2",3 )
/*33*/ test( "c43fIu1Mlz0K9hEVOgGcUdbeB5ksa7",3 )
/*34*/ test( "CAbYcW5VqHjzFdIkH_61PM0TsyRuie",3 )
/*35*/ test( "jInQnUvSayrJTsQWujovbbqW0STvoj",10 )
/*36*/ test( "iZDrvpUKgtj3FrZsZ4CLjrEgUdZzQG",11 )
/*37*/ test( "ROgYUOu6er_DA7nB6UGquO1GUHC62R",11 )
/*38*/ test( "Oh_be_a_fine_girl_kiss_me",9 )
/*39*/ test( "8086_6502_6809_Z80",11 )
/*40*/ test( "xcode_visualstudio_eclipse",11 )
/*41*/ test( "word_excel_powerpoint_outlook",9 )
/*42*/ test( "abcdefghijklmnopqrstuvwxyz0123",1 )
}