Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions BaekJoon/1509/gwangseok.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys
read_line = sys.stdin.readline


def update_is_palindrome(text, is_palindrome):
is_palindrome[0][0] = True

for idx in range(1, len(text)):
is_palindrome[idx][idx] = True
if text[idx-1] == text[idx]:
is_palindrome[idx-1][idx] = True

for length in range(2, len(text)):
for idx in range(len(text) - length):
if text[idx] == text[idx+length] and is_palindrome[idx+1][idx+length-1]:
is_palindrome[idx][idx+length] = True


def update_dp(dp, is_palindrome, txt, start_idx):
for idx in range(len(txt) - 1, start_idx, -1):
if is_palindrome[start_idx][idx]:
dp[idx] = min(dp[idx], dp[start_idx-1] + 1)

dp[start_idx] = min(dp[start_idx], dp[start_idx - 1] + 1)


text = read_line().strip()
dp = [2500] * len(text) + [0]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0을 추가한 건 dp를 갱신할 때 start_idx - 1을 처리하기 위해서인가요?

is_palindrome = [[False] * len(text) for _ in range(len(text))]

update_is_palindrome(text, is_palindrome)

for i in range(len(text)):
update_dp(dp, is_palindrome, text, i)

print(dp[len(text) - 1])