ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€?

๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•ต์‹ฌ ์›๋ฆฌ

๋ฌธ์ž์—ด ๋งค์นญ์„ ํ•˜๋ฉด์„œ ํ…์ŠคํŠธ(์ „์ฒด ๋ฌธ์ž์—ด)์™€ ํŒจํ„ด(์ฐพ๋Š” ๋ฌธ์ž์—ด)์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธ€์ž๊ฐ€ ๋‚˜์™”๋‹ค๋ฉด,

'์„œ๋กœ ๋‹ค๋ฅธ ๊ธ€์ž์˜ ๋ฐ”๋กœ ์ „ ๊นŒ์ง€๋Š” ์ผ์น˜ํ–ˆ๋‹ค'๋ผ๋Š” ์‚ฌ์‹ค๊ณผ '์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฐ™์€ ๋งŒํผ ๋น„๊ต๋ฅผ ๋œํ•ด๋„ ๋œ๋‹ค'๋Š” ์‚ฌ์‹ค ์ด์šฉํ•˜๊ธฐ

 

์ฆ‰, ๋ฐ”๋กœ ์ „ ๊ธ€์ž๊นŒ์ง€ ์ผ์น˜ํ–ˆ์—ˆ์œผ๋ฏ€๋กœ, ์ ‘๋ฏธ์‚ฌ์™€ ์ ‘๋‘์‚ฌ๊ฐ€  ๊ฐ™์€๋งŒํผ ๋น„๊ต๋ฅผ ๋œํ•˜๋„๋ก ์ด๋™์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

์ง์ ‘ ํ•˜๋‚˜ํ•˜๋‚˜ ์ดํ•ดํ•ด๋ณด๊ธฐ

 

์•„๋ž˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด์„œ ์ดํ•ดํ•ด๋ณด์ž.

@ @ @ @ a b c d a b c d  @ @ @ @   ์ธ ํ…์ŠคํŠธ์—์„œ, (@๋Š” ์•„๋ฌด ๊ธ€์ž๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž, ์‹ ๊ฒฝ์•ˆ์จ๋„ ๋œ๋‹ค.)

                a b c d a b c w z                   ์ธ ํŒจํ„ด์„ ์ฐพ๋Š”๋‹ค๊ณ  ํ•ด๋ณด์ž.

 

์ฒซ ๋ฌธ์ž๋ถ€ํ„ฐ ํ•˜๋‚˜ ํ•˜๋‚˜ ๋น„๊ต๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d a b c w z @ @ @ @                     

 

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d a b c w z@ @ @ @       

 

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d a b c w z  @ @ @ @      

 

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d a b c w z       

 

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d a b c w z       

 

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d b c w z       

 

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d b c w z       

 

์ˆœ์„œ๋Œ€๋กœ ๋น„๊ต๋ฅผ ํ•˜๋‹ค๊ฐ€ ํ…์ŠคํŠธ์˜ ๋ฌธ์ž(d)์™€ ํŒจํ„ด์˜ ๋ฌธ์ž(w)๊ฐ€ ๋‹ค๋ฅด๋‹ค!

์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋น›์„ ๋ฐœํ•œ๋‹ค.

 

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d b c w z       

 

์•ž์„œ ๋งํ–ˆ๋˜ KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•ต์‹ฌ ์›๋ฆฌ๋ฅผ ์ด์šฉํ•ด๋ณด์ž.

 

1. '์„œ๋กœ ๋‹ค๋ฅธ ๊ธ€์ž์˜ ๋ฐ”๋กœ ์ „ ๊นŒ์ง€๋Š” ์ผ์น˜ํ–ˆ๋‹ค'๋ผ๋Š” ์‚ฌ์‹ค

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d c w z       

์•ž์„œ์„œ ๋น„๊ตํ•œ ํŒŒ๋ž€ ๋ถ€๋ถ„๊นŒ์ง€๋Š” ์ผ์น˜ํ–ˆ๋‹ค. ์ฆ‰, w์˜ ๋ฐ”๋กœ ์ „๊นŒ์ง€๋Š” ๋น„๊ตํ•˜๋ฉด์„œ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์—†์—ˆ๋‹ค!

์ด ์‚ฌ์‹ค์„ ๋ช…์‹ฌํ•œ ์ƒํƒœ์—์„œ,

 

2.  '์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฐ™์€ ๋งŒํผ ๋น„๊ต๋ฅผ ๋œํ•ด๋„ ๋œ๋‹ค'๋Š” ์‚ฌ์‹ค

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b cc w z       

 

ํ˜„์žฌ ํŒจํ„ด์˜ ์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ abc๋กœ ๊ฐ™๋‹ค

๋ฐ”๋กœ ์œ— ๋ถ€๋ถ„์—์„œ ๋ดค๋“ฏ์ด, w ์ „๊นŒ์ง€๋Š” ๋ชจ๋‘ ์ผ์น˜ํ–ˆ๋‹ค. 

 

๊ทธ๋ ‡๋‹ค๋ฉด!!!!   ์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฐ™์€ ๋งŒํผ ๋น„๊ต๋ฅผ ๋œํ•ด๋„ ๋˜๊ฒ ๋‹ค

                                            =

์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฐ™์œผ๋‹ˆ๊นŒ(abc),

์ ‘๋‘์‚ฌ(์•ž์— ์žˆ๋Š” abc)๋ฅผ ์ ‘๋ฏธ์‚ฌ(๋’ค์— ์žˆ๋Š” abc)๊ฐ€ ์žˆ๋˜ ์œ„์น˜์— ๋†“์œผ๋ฉด ๋˜๊ฒ ๋„ค!

 

@ @ @ @ a b c d a b c d  @ @ @ @   

@ @ @ @ a b c d a b c w z       

@ @ @ @------->   a b c d a b c w z 

 

์ฆ‰, ์ด๋ ‡๊ฒŒ ๋œ๋‹ค. (๋˜‘๊ฐ™์€ ์ƒํ™ฉ์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด๋‹ค)

 

์ด ํ•ต์‹ฌ ์›๋ฆฌ๋ฅผ ์ดํ•ดํ–ˆ์œผ๋ฉด  KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ดํ•ดํ–ˆ๋‹ค๊ณ  ๋ณด๋ฉด๋œ๋‹ค.

ํ…์ŠคํŠธ์™€ ํŒจํ„ด์˜ ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅผ ๋•Œ๋งˆ๋‹ค ํ•œ ์นธ์”ฉ ์ด๋™ํ•ด์„œ ๋น„๊ตํ•˜๋Š” ๋‹จ์ˆœ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰๊ณผ๋Š” ๋‹ฌ๋ฆฌ

1. '์„œ๋กœ ๋‹ค๋ฅธ ๊ธ€์ž์˜ ๋ฐ”๋กœ ์ „ ๊นŒ์ง€๋Š” ์ผ์น˜ํ–ˆ๋‹ค'๋ผ๋Š” ์‚ฌ์‹ค
2.  '์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฐ™์€ ๋งŒํผ ๋น„๊ต๋ฅผ ๋œํ•ด๋„ ๋œ๋‹ค'๋Š” ์‚ฌ์‹ค

์ด ์‚ฌ์‹ค๋“ค์„ ์ด์šฉํ•ด ๋น„๊ตํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ํš๊ธฐ์ ์œผ๋กœ ์ค„์ด๊ฒŒ ๋œ๋‹ค.

 


๊ทธ๋Ÿฌ๋ฉด ํ•ต์‹ฌ ์›๋ฆฌ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰์„ ํ’€๋ฒ„์ „์œผ๋กœ ๊ฐ์ƒํ•ด๋ณด์ž. (์•„๋‹ˆ ๊ฐ์ƒ๋งŒํ•˜์ง€ ๋ง๊ณ  ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ์ƒ๊ฐํ•ด๋ณด์ž.)

ํ…์ŠคํŠธ "a b a c a a b a c c a b a c a b a a "  ์—์„œ

ํŒจํ„ด "a b a c a b"๋ฅผ ์ฐพ์•„๋ณด์ž

 

๊ทธ๋ฆผ 1

 

๊ทธ๋ฆผ 2: ์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฒน์น˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋’ค๋กœ ํ•œ ์นธ ์ด๋™

 

๊ทธ๋ฆผ 3

 

๊ทธ๋ฆผ 4: ์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฒน์น˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋’ค๋กœ ํ•œ ์นธ ์ด๋™

 


์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

์‚ฌ๋žŒ์€ ์œ„์˜ ๊ทธ๋ฆผ๋“ค ์ฒ˜๋Ÿผ ์˜†์œผ๋กœ ์˜ฎ๊ฒจ ๊ฐ€๋ฉด์„œ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ปดํ“จํ„ฐ๋Š” ์ด ๋ฐฉ์‹์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜๋Š” ์—†์„ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” ํ…์ŠคํŠธ์™€ ํŒจํ„ด์— ๊ฐ๊ฐ ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ณ , ํฌ์ธํ„ฐ๋ฅผ ์˜ฎ๊ฒจ ๊ฐ€๋ฉด์„œ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค.

์ฝ”๋“œ๋กœ ๋ณด๊ธฐ ์ „์— ๊ทธ๋ฆผ์œผ๋กœ ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•˜๋Š” ๋ฐฉ์‹์„ ์ดํ•ดํ•ด๋ณด์ž.

 

ํ…์ŠคํŠธ์˜ ํฌ์ธํ„ฐ์™€ ํŒจํ„ด์˜ ํฌ์ธํ„ฐ๋กœ ๋ฌธ์ž๋ฅผ ๋น„๊ตํ•˜๋ฉด์„œ, ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด

ํŒจํ„ด์˜ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ง€์ ์˜ ๋ฐ”๋กœ ์ „ ๋ฌธ์ž๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹คํŒจํ•จ์ˆ˜ ๊ฐ’(์ฃผํ™ฉ์ƒ‰ ์ˆซ์ž)์„ ์ด์šฉํ•ด,  ๊ทธ ๊ฐ’์„ ์ธ๋ฑ์Šค๋กœ ๊ฐ–๋Š” ๋ฌธ์ž๋กœ ์ด๋™ํ•œ๋‹ค.

์‹คํŒจ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ํฌ์ŠคํŒ…์„ ๋”ฐ๋กœ ํ•  ์˜ˆ์ •์ธ๋ฐ, ์‹คํŒจ ํ•จ์ˆ˜๋Š” ์ฃผํ™ฉ์ƒ‰ ์ˆซ์ž๋“ค์ด๋ฉฐ ํ•ด๋‹น ๋ฌธ์ž๊นŒ์ง€ ๋‚˜์˜ค๋Š” ์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฐ™์€ ์ตœ๋Œ€ ๊ธธ์ด์ด๋‹ค.

 

๊ทธ๋ฆผ 2-1


๊ทธ๋ฆผ 2-2


๊ทธ๋ฆผ 2-3


๊ทธ๋ฆผ 2-4


๊ทธ๋ฆผ 2-5, ์ตœ์ข…์ ์œผ๋กœ ํŒจํ„ด์˜ ํฌ์ธํ„ฐ๊ฐ€ ๋๊นŒ์ง€ ๋„์ฐฉํ–ˆ๋‹ค๋ฉด ๋ฌธ์ž์—ด์„ ์ฐพ์€ ๊ฒƒ์ด๋‹ค.

 

์ด์ œ ์ด ๋ฐฉ์‹์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž. (makeTable์€ ์‹คํŒจ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ์ธ๋ฐ, ์ด ๋ถ€๋ถ„์€ ๊ณง ์ถ”๊ฐ€๋กœ ํฌ์ŠคํŒ… ํ•  ์˜ˆ์ •.)

 

def makeTable(pattern):
    patternSize = len(pattern)
    table = [0 for i in range(patternSize)]
    j = 0
    for i in range (1, patternSize):
        while(j >0 and pattern[i] != pattern[j]):
            j = table[j-1]
        if(pattern[i] == pattern[j]):
            table[i] = j+1
            j += 1
    return table

def KMP(parent, pattern):
    table = makeTable(pattern)
    parentSize = len(parent)
    patternSize = len(pattern)
    parentPointer = 0
    patternPointer = 0
    count = 0
    for parentPointer in range(0, parentSize):
        while(patternPointer > 0 and parent[parentPointer] != pattern[patternPointer]):
            patternPointer = table[patternPointer - 1]
        if(parent[parentPointer] == pattern[patternPointer]):
            if(patternPointer == patternSize - 1):
                print(pattern, "์„", parent,"์—์„œ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค.")
                count += 1
                print("์ด", count, " ๋ฒˆ ๋‚˜์˜ต๋‹ˆ๋‹ค")
                patternPointer = table[patternPointer]
            else:
                patternPointer += 1
    if count == 0:
      print("ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์—†์Šต๋‹ˆ๋‹ค.")
    
    return count
    
KMP("abacaabaccabacabaa", "abacab")

 

๊ตฌํ˜„ ๊ฒฐ๊ณผ

 

abacab ์„ abacaabaccabacabaa ์—์„œ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค.
์ด 1  ๋ฒˆ ๋‚˜์˜ต๋‹ˆ๋‹ค


์‹œ๊ฐ„ ๋ณต์žก๋„

 

ํ…์ŠคํŠธ(๋ถ€๋ชจ)์˜ ๊ธธ์ด๋ฅผ N, ์ฐพ๋Š” ๋ฌธ์ž์—ด(ํŒจํ„ด)์˜ ๊ธธ์ด๋ฅผ M์ด๋ผ ํ•  ๋•Œ 

๋ฌธ์ž์—ด์„ ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ๋ชจ๋‘ ๋น„๊ตํ•˜๋Š” '๋‹จ์ˆœ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜'์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(NM)์ธ ๊ฒƒ์— ๋น„ํ•ด

'KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜'์€ O(N+M)์— ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

 

 

728x90
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ
250x250