๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 1543 - ๋ฌธ์„œ ๊ฒ€์ƒ‰

by HandHand 2021. 3. 18.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 1543๋ฒˆ

ํ’€์ด ๊ณผ์ •

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ๋ช‡ ๊ฐœ ์กด์žฌํ•˜๋Š”์ง€ ์ฐพ๋Š” ์™„์ „ ํƒ์ƒ‰ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์†๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด ๋น„๊ต ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•จ๊ป˜ ํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์ž…๋ ฅ์ด ํฌ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—

O(N^2) ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys

documents = sys.stdin.readline().strip()
word = sys.stdin.readline().strip()


def solution():
    answer, idx = 0, 0

    while idx <= len(documents) - len(word):
        is_matched = True

        for offset in range(len(word)):
            if documents[idx + offset] != word[offset]:
                is_matched = False
                break

        if is_matched:
            answer += 1
            idx += len(word)
        else:
            idx += 1

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 14923 - ๋ฏธ๋กœ ํƒˆ์ถœ  (0) 2021.03.18
BOJ 17406 - ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 4  (0) 2021.03.18
BOJ 11403 - ๊ฒฝ๋กœ ์ฐพ๊ธฐ  (0) 2021.03.18
BOJ 18243 - Small World Network  (0) 2021.03.18
BOJ 18238 - ZOAC2  (0) 2021.03.18

๐Ÿ’ฌ ๋Œ“๊ธ€