27 lines
616 B
Python
27 lines
616 B
Python
S = input().rstrip()
|
|
L = len(S)
|
|
|
|
isPal = [[False]*L for _ in range(L)]
|
|
for i in range(L) :
|
|
isPal[i][i] = True
|
|
if i != 0 and S[i] == S[i-1] :
|
|
isPal[i-1][i] = True
|
|
|
|
for l in range(3, L+1) :
|
|
for start in range(L-l+1) :
|
|
end = start + l - 1
|
|
|
|
if S[start] == S[end] and isPal[start+1][end-1] :
|
|
isPal[start][end] = True
|
|
|
|
dp = [L+1] * L
|
|
|
|
for end in range(l) :
|
|
for start in range(end+1) :
|
|
if isPal[start][end] :
|
|
if start == 0 :
|
|
dp[end] = 1
|
|
else :
|
|
dp[end] = min(dp[end], dp[start-1] + 1)
|
|
|
|
print(dp[L-1]) |