[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 11478 - ์„œ๋กœ ๋‹ค๋ฅธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜

keyneneยท2022๋…„ 10์›” 15์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
9/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 11478 -
์„œ๋กœ ๋‹ค๋ฅธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜

๐Ÿ“œ๋ฌธ์ œ


๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

๋ฌธ์ž์—ด s๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  s[i:j]์™€ set()์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ์„œ ์ €์žฅํ•˜์ž
(set์€ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋ฏ€๋กœ ์ค‘๋ณต์ผ€์ด์Šค๋Š” ์ œ๊ฑฐํ•˜์—ฌ ์ถœ๋ ฅํ•ด์ค„ ๊ฒƒ์ž„!)


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. s๋ฌธ์ž์—ด ์ž…๋ ฅ๋ฐ›๊ณ  s_set() ๋นˆ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ณ ,
    1์ค‘์ฒฉ n์„ len(s)๋งŒํผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ ,
    2์ค‘์ฒฉ i๋ฅผ 1~len(s)+1๋งŒํผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ
    s[n:n+i]๊ฐ’์„ s_set์— ์ €์žฅํ•˜์ž
โ€ปs[n:n+1]์ธ ์ด์œ ?
  s[0:1] = s๋ฌธ์ž์—ด์„ 0~1๋งŒํผ ์ž˜๋ผ์คŒ!
  ...์ž์„ธํ•œ๊ฑด ์•„๋ž˜ ์ฝ”๋“œ์—์„œ ์„ค๋ช…ํ•˜๊ฒ ์Œ

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline

s = input().rstrip()
s_set = set()

for n in range(len(s)):
    for i in range(1,len(s)+1):
        s_set.add(s[n:n+i])

print(len(s_set))

โœ๏ธ1. ์ž…๋ ฅ๊ฐ’๋ฐ›์•„ ์ž๋ฆฟ์ˆ˜๋งŒํผ ์ž๋ฅด๊ณ , ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์—ฌ ์ถœ๋ ฅ

#1. ์ž…๋ ฅ๊ฐ’๋ฐ›๊ธฐ
import sys
input = sys.stdin.readline

s = input().rstrip()

#3. ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ
s_set = set()

#2. ์ž๋ฆฟ์ˆ˜๋งŒํผ ์ž๋ฅด๊ธฐ
for n in range(len(s)):
    for i in range(1,len(s)+1):
        s_set.add(s[n:n+i])

print(len(s_set))
์ €์žฅํ•ด์•ผํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋‚˜์—ดํ•ด๋ณด์ž (s = 'ababc'๋ผ๊ณ  ๊ฐ€์ •)
 1์ž๋ฆฟ์ˆ˜ : S[0:1], S[1:2], S[2:3], S[3:4], S[4:5]
 2์ž๋ฆฟ์ˆ˜ : S[0:2], S[1:3], S[2:4], S[3:5]
 3์ž๋ฆฟ์ˆ˜ : S[0:3], S[1:4], S[2:5]
 4์ž๋ฆฟ์ˆ˜ : S[0:4], S[1:5]
 5์ž๋ฆฟ์ˆ˜ : S[0:5]
 
 1. s ๋ฌธ์ž์—ด์˜ ์ž๋ฆฟ์ˆ˜๋งŒํผ for๋ฌธ์„ ๋Œ๋ ค์•ผ๊ฒŸ๋‹ค
  ๐Ÿ‘‰๐Ÿป for n in range(len(s))
  
 2. ์ž๋ฆฟ์ˆ˜๋งˆ๋‹ค 1,2,3,4,5 / 2,3,4,5 / 3,4,5 ... ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋ ค๋ฉด?
 	for n in range(len(s)):
      for i in range(len(s)):
      	s_set.add(s[n:j]) 
     ๐Ÿ‘‰๐Ÿป ์—ฌ๊ธฐ์„œ j๋Š”??? i์—์„œ ์ž๋ฆฟ์ˆ˜(n)๋งŒํผ ์ฆ๊ฐ€ํ•˜๊ณ  ์žˆ์Œ!!
        โ€ป j = n+i

๐Ÿ“š์ •๋ฆฌ

  1. ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ๋Š”? s[i:j]
  2. ์ง‘ํ•ฉ์€ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€