๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด BOJ #5582

by seolhee2750 2022. 5. 24.
๋ฌธ์ œ

https://www.acmicpc.net/problem/5582

 

5582๋ฒˆ: ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด

๋‘ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ๋ฌธ์ž์—ด์— ๋ชจ๋‘ ํฌํ•จ๋œ ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์–ด๋–ค ๋ฌธ์ž์—ด s์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด t๋ž€, s์— t๊ฐ€ ์—ฐ์†์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import sys

a = sys.stdin.readline().strip()
b = sys.stdin.readline().strip()
cnt = [[0 for _ in range(len(b) + 1)] for _ in range(len(a) + 1)]

for i in range(len(a)):
    for j in range(len(b)):
        if a[i] == b[j]:
            cnt[i+1][j+1] = cnt[i][j] + 1

print(max(map(max, cnt)))

๐Ÿ‘‰ DP, ๋ฌธ์ž์—ด ๋ฌธ์ œ

2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๋Œ€๊ฐ์„ ์œผ๋กœ ์—ฐ์†๋˜๋Š” ๋ฌธ์ž์—ด์„ ์นด์šดํŠธํ•ด์„œ ๋ˆ„์ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ

์‹œ๊ฐ„์ดˆ๊ณผ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋ฅผ ์ฃผ์˜ํ•ด์„œ ํ’€์–ด์•ผ ํ•œ๋‹ค.

๊ทผ๋ฐ ๋‚œ ์•„๋ฌด๋ฆฌ ํ’€์–ด๋„ ํ•œ๊ณ„๊ฐ€ ์žˆ์–ด์„œ ๊ฒฐ๊ตญ pypy๋กœ ์ œ์ถœํ–ˆ๋‹ค.ใ…œ

๋‹ค์Œ์— ๋‹ค์‹œ ํ•œ ๋ฒˆ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค,,

๋Œ“๊ธ€