baekjoon 20260415

This commit is contained in:
songyc macbook 2026-04-15 22:43:14 +09:00
parent 7a2f8750f6
commit 6270575be8
2 changed files with 18 additions and 20 deletions

View File

@ -1,30 +1,27 @@
s = input().rstrip() S = input().rstrip()
l = len(s) L = len(S)
isPalindrom = [[False]*l for _ in range(l)] isPal = [[False]*L for _ in range(L)]
for length in range(1,l+1) : for i in range(L) :
for start in range(l-length + 1) : isPal[i][i] = True
end = start + length - 1 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 length == 1 : if S[start] == S[end] and isPal[start+1][end-1] :
isPalindrom[start][end] = True isPal[start][end] = True
elif length == 2 : dp = [L+1] * L
if s[start] == s[end] :
isPalindrom[start][end] = True
else :
if s[start] == s[end] and isPalindrom[start+1][end-1]:
isPalindrom[start][end] = True
dp = [2501]*l
for end in range(l) : for end in range(l) :
for start in range(end+1) : for start in range(end+1) :
if isPalindrom[start][end] : if isPal[start][end] :
if start == 0 : if start == 0 :
dp[end] = 1 dp[end] = 1
else : else :
dp[end] = min(dp[end], dp[start-1] + 1) dp[end] = min(dp[end], dp[start-1] + 1)
print(dp[l-1]) print(dp[L-1])

View File

@ -0,0 +1 @@
print("\n".join([" "*i + "*"*(2*N-(2*i+1)) for N in [int(input())] for i in range(N)]))