[BOJ][๐ŸŸก5][๋ฐฑ์ค€#05582] ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #5582


๋ฌธ์ œ

๋‘ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ๋ฌธ์ž์—ด์— ๋ชจ๋‘ ํฌํ•จ๋œ ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์–ด๋–ค ๋ฌธ์ž์—ด s์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด t๋ž€, s์— t๊ฐ€ ์—ฐ์†์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด ABRACADABRA์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ABRA, RAC, D, ACADABRA, ABRACADABRA, ๋นˆ ๋ฌธ์ž์—ด ๋“ฑ์ด๋‹ค. ํ•˜์ง€๋งŒ, ABRC, RAA, BA, K๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋‹ค. ๋‘ ๋ฌธ์ž์—ด ABRACADABRA์™€ ECADADABRBCRDARA์˜ ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ CA, CADA, ADABR, ๋นˆ ๋ฌธ์ž์—ด ๋“ฑ์ด ์žˆ๋‹ค. ์ด ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ADABR์ด๋ฉฐ, ๊ธธ์ด๋Š” 5์ด๋‹ค. ๋˜, ๋‘ ๋ฌธ์ž์—ด์ด UPWJCIRUCAXIIRGL์™€ SBQNYBSBZDFNEV์ธ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ๋นˆ ๋ฌธ์ž์—ด์ด๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„๊ณผ ๋‘˜์งธ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์€ ๋Œ€๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 1 ์ด์ƒ 4000 ์ดํ•˜์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋‘ ๋ฌธ์ž์—ด์— ๋ชจ๋‘ ํฌํ•จ ๋œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

ABRACADABRA
ECADADABRBCRDARA


์ถœ๋ ฅ

5


์˜ˆ์ œ 2

์ž…๋ ฅ

UPWJCIRUCAXIIRGL
SBQNYBSBZDFNEV


์ถœ๋ ฅ

0


My Sol

def LCS(i, j):
    global memo, maxl
    if not i*j: return
    elif A[i]==B[j]:
        memo[i][j] = memo[i-1][j-1]+1
        maxl = max(maxl, memo[i][j])


A = '.'+input()
B = '.'+input()
I, J = len(A), len(B)
maxl = 0

memo = [[0]*J for _ in range(I)]
for i in range(I):
    for j in range(J):
        LCS(i, j)

print(maxl)

์ฒ˜์Œ์œผ๋กœ DP๋ฅผ ์ด์šฉํ•ด ํ’€์–ด๋ณธ LCS ๋ฌธ์ œ์˜€๋‹ค. ๋ชจ๋“  ์ž๋ฆฌ์˜ ์•ŒํŒŒ๋ฒณ์— ๋Œ€ํ•˜์—ฌ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด 0, ์ผ์น˜ํ•œ๋‹ค๋ฉด ๊ฐ๊ฐ์˜ ์ด์ „ ์•ŒํŒŒ๋ฒณ๊นŒ์ง€์˜ ์—ฐ์† ๊ธธ์ด + 1์˜ ๊ฐ’์„ memo์— ๋ถ€์—ฌํ•œ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ, ์ผ์น˜ํ•  ๋•Œ๋งˆ๋‹ค ์ „์—ญ์˜ maxl๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ฐฑ์‹ ํ•ด์ฃผ์—ˆ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ