Algorithm/SWEA

[SWEA - D3] 1213. String

hello_u 2023. 5. 4. 20:42

 

 μ£Όμ–΄μ§€λŠ” λ¬Έμžμ—΄μ—μ„œ νŠΉμ •ν•œ λ¬Έμžμ—΄μ˜ 개수λ₯Ό λ°˜ν™˜ν•˜κΈ°κΈ° Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasicsofahealthydietandgoodnutrition.
μœ„ λ¬Έμž₯μ—μ„œ ti λ₯Ό κ²€μƒ‰ν•˜λ©΄, 닡은 4이닀.

 

λ‚˜μ˜ μ½”λ“œ

for _ in range(1, 11):
    t = int(input())
    s1 = input()
    s2 = input()
    s1L = len(s1)
    s2L = len(s2)
    cnt = 0
    for i in range(s2L-s1L+1):
        if s2[i:s1L+i] == s1:
            cnt += 1
    print("#"+str(t) , cnt )

 

νŒŒμ΄μ¬μ—λŠ” count ν•¨μˆ˜κ°€ μžˆλ‹€. 

생각 μ•ˆλ‚˜μ„œ λ¬Έμžμ—΄ μŠ¬λΌμ΄μ‹±ν•΄μ„œ 반볡문돌리고 λΉ„κ΅ν•΄μ„œ ν’€μ—ˆλ‹€. 

 

count ν•¨μˆ˜λ₯Ό μ΄μš©ν•œ 풀이

for _ in range(10):
    n = int(input())
    find_str = input()
    s = input()
    print(f'#{n} {s.count(find_str)}')