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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

14426๋ฒˆ: ์ ‘๋‘์‚ฌ ์ฐพ๊ธฐ

๋ฌธ์ž์—ด S์˜ ์ ‘๋‘์‚ฌ๋ž€ S์˜ ๊ฐ€์žฅ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, S = "codeplus"์˜ ์ ‘๋‘์‚ฌ๋Š” "code", "co", "codepl", "codeplus"๊ฐ€ ์žˆ๊ณ , "plus", "s", "cude", "crud"๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ด N๊ฐœ์˜ ๋ฌธ์ž

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 M๊ฐœ์˜ ๋ฌธ์ž๋“ค ์ค‘ N๊ฐœ์˜ ๋ฌธ์ž์˜ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ๋˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ ‘๋ฏธ์‚ฌ ์ด๋ฏ€๋กœ ์ฐพ๊ณ ์ž ํ•˜๋Š” ํŒจํ„ด์˜ ์ˆ˜์™€ N๊ฐœ์˜ ๋ฌธ์ž ์ค‘ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

split์„ ํ™œ์šฉํ•œ ํ’€์ด

from sys import stdin


if __name__ == '__main__':
    n, m = map(int, stdin.readline().split())
    strings = [stdin.readline().strip() for _ in range(n)]
    cnt = 0

    for _ in range(m):
        pattern = stdin.readline().strip()
        for s in strings:
            if pattern == s[:len(pattern)]:
                cnt += 1
                break
    print(cnt)

 

startswith์„ ํ™œ์šฉํ•œ ํ’€์ด

from sys import stdin


if __name__ == '__main__':
    n, m = map(int, stdin.readline().split())
    strings = {stdin.readline().strip() for _ in range(n)}
    cnt = 0

    for _ in range(m):
        pattern = stdin.readline().strip()
        for s in strings:
            if s.startswith(pattern):
                cnt += 1
                break
    print(cnt)

 ๋‘ ๋ฐฉ์‹์€ ๋™์ผํ•˜์ง€๋งŒ ์ด๋ฒˆ์— ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ `startswith`์ด๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋ฐฉ์‹์€ ๋™์ผํ•˜์ง€๋งŒ `split`์€ 712ms, `startswith`์€ 932ms์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜์—ˆ๋‹ค. ํ•ด๋‹น ์ฝ”๋“œ๋“ค์€ `PyPy3`๋กœ ์ œ์ถœํ•˜๋ฉด ํ†ต๊ณผํ•˜์ง€๋งŒ `Python3`์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.๐Ÿ˜ฅ

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€