結果

問題 No.273 回文分解
コンテスト
ユーザー shi-mo
提出日時 2016-09-10 16:22:25
言語 Ruby
(3.4.1)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 372 bytes
コンパイル時間 41 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-06-25 13:41:04
合計ジャッジ時間 4,437 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

s = gets.chomp

n = s.length

def is_valid?(s, i, j)
  len = i - j
  return true if 1 == len

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

ans = [1]
0.upto(n-1) do |i|
  i.upto(n) do |j|
    ans << (j-i) if is_valid?(s, i, j)
  end
end
puts (ans - [n]).max
0