๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 14426_์ ‘๋‘์‚ฌ ์ฐพ๊ธฐ

๋ฟŒ์•ผ._. 2021. 12. 27. 11:12

Silver III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/14426)

<์ ‘๋‘์‚ฌ ์ฐพ๊ธฐ>

๋ฌธ์ œ

 

๋ฌธ์ž์—ด S์˜ ์ ‘๋‘์‚ฌ๋ž€ S์˜ ๊ฐ€์žฅ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, S = "codeplus"์˜ ์ ‘๋‘์‚ฌ๋Š” "code", "co", "codepl", "codeplus"๊ฐ€ ์žˆ๊ณ , "plus", "s", "cude", "crud"๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ์•„๋‹ˆ๋‹ค.
์ด N๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” M๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘์—์„œ ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด ์ค‘ ์ ์–ด๋„ ํ•˜๋‚˜์˜ ์ ‘๋‘์‚ฌ์ธ ๊ฒƒ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ N๊ณผ M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค.
๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค.
๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค.
์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 500์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ์ง‘ํ•ฉ S์— ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

 

์ถœ๋ ฅ

 

์ฒซ์งธ ์ค„์— M๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘์— ์ด ๋ช‡ ๊ฐœ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด ์ค‘ ์ ์–ด๋„ ํ•˜๋‚˜์˜ ์ ‘๋‘์‚ฌ์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋ฌธ์ œ ํ’€์ด
- ์‹œ๊ฐ„ ์ดˆ๊ณผ ์ฝ”๋“œ

import sys

if __name__=='__main__':
    N,M=map(int, sys.stdin.readline().split())

    temp=[]
    for i in range(N):
        temp.append(sys.stdin.readline().strip())

    cnt=0
    for i in range(M):
        find=sys.stdin.readline().strip()
        for j in temp:
            pre=j[:len(find)]
            if find==pre:
                cnt+=1
                break

    print(cnt)

 

  • ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉ- ์ฐพ์„ ์ ‘๋‘์‚ฌ ์ž…๋ ฅ๋ฐ›๊ณ  ๊ทธ ๊ธธ์ด๋งŒํผ ๋ฌธ์ž์—ด ์ž˜๋ผ์„œ ๋น„๊ตํ•˜์—ฌ ๊ฐ™์œผ๋ฉด cnt +1

์ด ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ ์ฐพ์•„๋ณด๋‹ˆ ๊ธ€์ด 3๊ฐœ ์ •๋„๋ฐ–์— ์—†์—ˆ๋‹ค. ์–ด๋–ค ๋ถ„๋„ ์ด๋ ‡๊ฒŒ ํ’€์–ด PyPy3๋กœ ํ†ต๊ณผํ•˜์˜€๋‹ค๊ณ  ํ•ด์„œ ์ผ๋‹จ ๋Œ๋ ค๋ณด์•˜๋”๋‹ˆ
ํ†ต๊ณผ... ใ…Ž_ใ…Ž ํ•˜์ง€๋งŒ ๋‚œ Python3๋กœ ํ’€๊ธฐ์—.. ๋‹ค์‹œ ์ฐพ์•„์„œ ๋„์ „!


- ํ†ต๊ณผํ•œ ์ฝ”๋“œ

import sys

if __name__=='__main__':
    N,M=map(int, sys.stdin.readline().strip().split())

    temp=[]
    for i in range(N):
        temp.append(sys.stdin.readline().strip()) # ๋ฌธ์ž์—ด ์ž…๋ ฅ

    find=[]
    for i in range(M):
        find.append(sys.stdin.readline().strip()) # ์ ‘๋‘์‚ฌ ์ž…๋ ฅ

    #์ •๋ ฌ
    find.sort()
    temp.sort()

    cnt=0
    s=0
    for i in find:
        for j in range(s,N):
            if i==temp[j][:len(i)]:
                cnt+=1
                s=j # ์ •๋ ฌ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— index ๊ต์ฒด
                break

    print(cnt)

 

  • ๋ฌธ์ž์—ด, ์ ‘๋‘์‚ฌ ์ž…๋ ฅ๋ฐ›์€ ํ›„ ์ •๋ ฌ
  • ํŠน์ • ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ํ™•์ธ
  • ์ ‘๋‘์‚ฌ ๋งž๋‹ค๋ฉด cnt ์ฆ๊ฐ€ ๋ฐ for๋ฌธ ์‹œ์ž‘ํ•˜๋Š” index ์ˆ˜์ •

์ƒ๊ฐ๐Ÿค”


์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ํ•ต์‹ฌ ํฌ์ธํŠธ๋Š” ์ •๋ ฌ๊ณผ for๋ฌธ์˜ index ์ˆ˜์ •์ธ ๊ฒƒ ๊ฐ™๋‹ค.
์กฐ๊ธˆ๋งŒ ์ƒ๊ฐ ๋” ํ•ด๋ณด๋ฉด ์ •๋ ฌ์„ ์ƒ๊ฐํ•ด๋‚ผ ์ˆ˜ ์žˆ์—ˆ์„ ํ…๋ฐ ๋ฐ”๋กœ ์ฐพ์•„๋ณธ ๋‚˜์˜... ์†๊ฐ€๋ฝ์— ํ›„ํšŒํ•œ๋‹ค ^_^