Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/PythonํŒŒ์ด์ฌ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง(2018 ์นด์นด์˜ค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ 1์ฐจ)

meeeeejin 2021. 5. 3. 21:52

๋ฌธ์ œ ๋งํฌ: programmers.co.kr/learn/courses/30/lessons/17677

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง

๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง ์—ฌ๋Ÿฌ ์–ธ๋ก ์‚ฌ์—์„œ ์Ÿ์•„์ง€๋Š” ๋‰ด์Šค, ํŠนํžˆ ์†๋ณด์„ฑ ๋‰ด์Šค๋ฅผ ๋ณด๋ฉด ๋น„์Šท๋น„์Šทํ•œ ์ œ๋ชฉ์˜ ๊ธฐ์‚ฌ๊ฐ€ ๋งŽ์•„ ์ •์ž‘ ํ•„์š”ํ•œ ๊ธฐ์‚ฌ๋ฅผ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค. Daum ๋‰ด์Šค์˜ ๊ฐœ๋ฐœ ์—…๋ฌด๋ฅผ ๋งก๊ฒŒ ๋œ ์‹ ์ž…์‚ฌ์› ํŠœ๋ธŒ

programmers.co.kr

 

 

๋ฌธ์ œ ์„ค๋ช…

๋‘ ์ง‘ํ•ฉ A, B ์‚ฌ์ด์˜ ์ž์นด๋“œ ์œ ์‚ฌ๋„ J(A, B)๋Š” ๋‘ ์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ ํฌ๊ธฐ๋ฅผ ๋‘ ์ง‘ํ•ฉ์˜ ํ•ฉ์ง‘ํ•ฉ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์œผ๋กœ ์ •์˜๋œ๋‹ค. 

(ํ•ฉ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ) = (A์˜ ํฌ๊ธฐ + B์˜ ํฌ๊ธฐ - ๊ต์ง‘ํ•ฉ์˜ ํฌ๊ธฐ) ์‹์ด ๋งŒ์กฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ตญ ์ž์นด๋“œ ์œ ์‚ฌ๋„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. 

J(A, B) = (๊ต์ง‘ํ•ฉ์˜ ํฌ๊ธฐ) / (A์˜ ํฌ๊ธฐ + B์˜ ํฌ๊ธฐ - ๊ต์ง‘ํ•ฉ์˜ ํฌ๊ธฐ)

์ฆ‰, ๊ต์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒ ๊ตฌํ•˜๋ฉด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๋‹ค์ค‘์ง‘ํ•ฉ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ €์žฅํ–ˆ๋‹ค. ๋ณ€ํ™˜ ๊ณผ์ •์—์„œ ์˜๋ฌธ์ž์˜ ๊ฒฝ์šฐ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ์ €์žฅํ–ˆ๋‹ค. (๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ฐ™์€ ๋ฌธ์ž๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ ์œ„ํ•จ) 

 

์ฃผ์˜ํ•  ์ ์€ A, B๊ฐ€ ๋ชจ๋‘ ๊ณต์ง‘ํ•ฉ์ธ ๊ฒฝ์šฐ J(A, B)๋ฅผ 1๋กœ ๋ฌธ์ œ์—์„œ ์ •์˜ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  ๋‘ ์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์„œ ์ž์นด๋“œ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ณ  65536์„ ๊ณฑํ•ด์„œ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค. ๊ต์ง‘ํ•ฉ ํฌ๊ธฐ๋Š” ๋‘ ๋‹ค์ค‘์ง‘ํ•ฉ A, B ์ค‘์—์„œ ๋” ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ์ง‘ํ•ฉ์œผ๋กœ ๋†“๊ณ (์ค‘๋ณต์„ฑ์„ ์ œ๊ฑฐํ•˜๊ณ ) ๊ต์ง‘ํ•ฉ์„ ์ฐพ์•˜๋‹ค. ์–ด๋–ค ์›์†Œ x๊ฐ€ A์™€ B์— ๋ชจ๋‘ ์†ํ•˜๋Š” ๊ฒฝ์šฐ A, B์—์„œ x๊ฐ€ ๋” ์ ๊ฒŒ ํฌํ•จ๋œ ์ˆ˜๋ฅผ ๊ต์ง‘ํ•ฉ ํฌ๊ธฐ์— ๋”ํ–ˆ๋‹ค. 

 

 

 

 

 

์†Œ์Šค์ฝ”๋“œ

def numIntersection(A, B):
    n = 0   # A์™€ B์˜ ๊ต์ง‘ํ•ฉ ์›์†Œ ๊ฐœ์ˆ˜
    # A์˜ ์›์†Œ ๊ฐœ์ˆ˜ <= B์˜ ์›์†Œ ๊ฐœ์ˆ˜
    for x in set(A):
        if x in B:  # x๊ฐ€ A์™€ B ๋ชจ๋‘์— ์†ํ•˜๋Š” ๊ฒฝ์šฐ
            n += min(A.count(x), B.count(x))
    return n;

def solution(str1, str2):
    answer = 0
    # ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ณ  ๋‹ค์ค‘์ง‘ํ•ฉ ์ƒ์„ฑ
    str1 = str1.lower()
    str2 = str2.lower()
    A, B = [], []
    for i in range(len(str1) - 1):
        # ์˜๋ฌธ์ž์ธ ๊ฒฝ์šฐ์—๋งŒ ๋‹ค์ค‘์ง‘ํ•ฉ์˜ ์›์†Œ๋กœ ํฌํ•จ
        if str1[i].isalpha() and str1[i + 1].isalpha():
            A.append(str1[i] + str1[i + 1])
    for i in range(len(str2) - 1):
        if str2[i].isalpha() and str2[i + 1].isalpha():
            B.append(str2[i] + str2[i + 1])
    # A, B ๋ชจ๋‘๊ฐ€ ๊ณต์ง‘ํ•ฉ์ธ ๊ฒฝ์šฐ ์ž์นด๋“œ ์œ ์‚ฌ๋„๋Š” 1
    if (not A) and (not B): return 65536
    # ๊ต์ง‘ํ•ฉ ์›์†Œ์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    n = numIntersection(A, B) if len(A) < len(B) else numIntersection(B, A)
    answer = n / (len(A) + len(B) - n)

    return int(answer*65536)

 

 

 

 

 

 

 

728x90