結果

問題 No.273 回文分解
ユーザー shi-moshi-mo
提出日時 2016-09-10 16:02:32
言語 Ruby
(3.3.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 883 bytes
コンパイル時間 72 ms
コンパイル使用メモリ 11,456 KB
実行使用メモリ 15,344 KB
最終ジャッジ日時 2023-09-07 19:54:48
合計ジャッジ時間 4,400 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
15,024 KB
testcase_01 AC 89 ms
15,300 KB
testcase_02 AC 83 ms
15,324 KB
testcase_03 AC 83 ms
15,120 KB
testcase_04 AC 81 ms
15,240 KB
testcase_05 AC 87 ms
15,240 KB
testcase_06 AC 84 ms
15,124 KB
testcase_07 AC 84 ms
15,028 KB
testcase_08 AC 84 ms
15,020 KB
testcase_09 AC 82 ms
15,160 KB
testcase_10 AC 82 ms
15,132 KB
testcase_11 AC 81 ms
15,160 KB
testcase_12 AC 82 ms
15,048 KB
testcase_13 AC 82 ms
15,096 KB
testcase_14 AC 88 ms
15,320 KB
testcase_15 AC 81 ms
15,024 KB
testcase_16 AC 81 ms
15,328 KB
testcase_17 AC 82 ms
15,192 KB
testcase_18 AC 81 ms
15,120 KB
testcase_19 AC 84 ms
15,344 KB
testcase_20 AC 84 ms
15,048 KB
testcase_21 AC 81 ms
15,164 KB
testcase_22 AC 90 ms
15,028 KB
testcase_23 AC 89 ms
15,096 KB
testcase_24 AC 93 ms
15,344 KB
testcase_25 AC 88 ms
15,096 KB
testcase_26 AC 88 ms
15,232 KB
testcase_27 AC 89 ms
15,064 KB
testcase_28 AC 90 ms
15,064 KB
testcase_29 AC 88 ms
15,044 KB
testcase_30 AC 90 ms
15,244 KB
testcase_31 AC 82 ms
15,120 KB
testcase_32 AC 81 ms
15,208 KB
testcase_33 AC 83 ms
15,328 KB
testcase_34 AC 86 ms
15,204 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

s = gets.chomp

n = s.length
$dp_valid  = Array.new(n+1){ Array.new(n+1) }
$dp_answer = Array.new(n+1){ Array.new(n+1) }

def is_valid?(s, i, j)
  return $dp_valid[i][j] unless $dp_valid[i][j].nil?

  len = i - j
  return ($dp_valid[i][j] = true) if 1 == len

  center = (i + j) / 2
  if 0 == (len % 2)
    return $dp_valid[i][j] = (s[i..(center-1)] == s[center..(j-1)].reverse)
  end
  return $dp_valid[i][j] = (s[i..(center-1)] == s[(center+1)..(j-1)].reverse)
end

def solve(s, i, j)
  return $dp_answer[i][j] if $dp_answer[i][j]

  if i == j
    return $dp_answer[i][j] = 0
  end
  if is_valid?(s, i, j)
    return $dp_answer[i][j] = j - i
  end

  ans = 0
  (i+1).upto(j-1) do |k|
    ans = [ans, [solve(s, i, k), solve(s, k, j)].max].max
  end
  return $dp_answer[i][j] = ans
end

ans = 0
1.upto(n-1) do |k|
  ans = [ans, [solve(s, 0, k), solve(s, k, n)].max].max
end
puts ans
0