๐Ÿง ๋ฐฑ์ค€ 14425

๐Ÿค” Algorithm ๊ณผ์ • 
1. s์™€ string ๋ฆฌ์ŠคํŠธ์— ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ๊ฐ์˜ ๋ฌธ์ž์—ด์„ ๋„ฃ์Œ
2. for๋ฌธ์œผ๋กœ string์„ ๋Œ๋ฉด์„œ string์— ์žˆ๋Š” ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ s์— ์žˆ๋‹ค๋ฉด?
3. cnt๊ฐ’ 1 ์ฆ๊ฐ€ (= ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์žˆ๋‹ค๋Š” ๊ฒƒ)
4. ์ตœ์ข…์ ์œผ๋กœ cnt๊ฐ’ ์ถœ๋ ฅ


๐Ÿคซ  solution_14425

N, M = map(int, input().split())
s = [input() for _ in range(N)]
string = [input() for _ in range(M)]

cnt = 0
for i in string:
    if i in s:
        cnt += 1
print(cnt)

 

 

 

๐Ÿง ๋ฐฑ์ค€ 10815, 10816(์ด์ง„ํƒ์ƒ‰)

๐Ÿค” Algorithm ๊ณผ์ • 
1. ์ค‘๋ณต๋˜์–ด ์ž…๋ ฅ๋˜์–ด๋„ ๋น„๊ต๋งŒ ํ•˜๋ฉด ๋˜๊ธฐ์— n์ด๋ผ๋Š” set๊ณผ m์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ
2. ์ดํ›„ m์•ˆ์˜ ์›์†Œ๋ฅผ ๋Œ๋ฉด์„œ ๋งŒ์•ฝ m์˜ ์›์†Œ๊ฐ€ n์— ์žˆ๋‹ค๋ฉด 1์„ ์ถœ๋ ฅ, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
3. ๋ฌผ๋ก  ์œ„์˜ ๊ฒฝ์šฐ m ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ์— ์ˆœ์„œ๋Œ€๋กœ ์ ‘๊ทผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์„œ๋ฅผ ์ƒ๊ด€์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.


๐Ÿคซ  solution_10815

# cf. n์„ list๋กœ ํ–ˆ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์Œ.
# ์ฆ‰, ์ค‘๋ณต๋œ ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜์–ด set๋ณด๋‹ค ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋” ๊ฑธ๋ฆฌ๊ฒŒ ๋˜๋Š” ๊ฒƒ.
N, n = input(), set(map(int, input().split()))
M, m = input(), list(map(int, input().split()))
m_set = list(set(m))

for i in m:
    print(1, end = " ") if i in n else print(0, end = " ")



# ๋ฆฌ์ŠคํŠธ๋ฅผ ์‹ค์ œ๋กœ ๋ถ„ํ• ํ•ด์„œ ์ƒˆ๋กœ ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ O(N)์œผ๋กœ ๋งค์šฐ ๋ฌด๊ฑฐ์šด ์—ฐ์‚ฐ

 



๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ(10816): ์ด์ง„ํƒ์ƒ‰

๐Ÿค” count ํ•จ์ˆ˜๋ฅผ ๋ฌดํ„ฑ๋Œ€๊ณ  ์“ฐ๋ฉด ์•ˆ๋œ๋‹ค??
countํ•จ์ˆ˜๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n)์‹œ๊ฐ„์ด ๊ฑธ๋ ค์„œ for๋ฌธ๊ณผ ๊ฐ™์ด ์“ฐ๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ๋†’๋‹ค.

๐Ÿค”
 Algorithm ๊ณผ์ • 
1. cnt = {}๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
2. cnt๋ผ๋Š” ๋น„์–ด์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— i ์ฆ‰, n์˜ ์›์†Œ๊ฐ€ ์—†๋‹ค๋ฉด cnt[i] = 1๋กœ ์›์†Œ์™€ ๊ฐœ์ˆ˜๋ฅผ ๋งตํ•‘
3. ๋งŒ์•ฝ i in cnt๋ผ๋ฉด ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ค˜์•ผ ํ•˜๋ฏ€๋กœ cnt[i] += 1์„ ํ†ตํ•ด value(์›์†Œ์˜ ๊ฐœ์ˆ˜)๋ฅผ ์ฆ๊ฐ€
4. ์ดํ›„ m ์•ˆ์˜ ์–ด๋–ค ์›์†Œ i์— ๋Œ€ํ•ด cnt์— i๊ฐ€ ์žˆ์œผ๋ฉด value๋ฅผ ์ถœ๋ ฅ, ์—†์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.




๐Ÿคซ  solution_10816(์‹œ๊ฐ„์ดˆ๊ณผ  โˆต countํ•จ์ˆ˜์˜ ์‚ฌ์šฉ)

N, n = int(input()), list(map(int, input().split()))
M, m = int(input()), list(map(int, input().split()))

for i in m:
    print(n.count(i), end = " ") if i in n else print(0, end = " ")


๐Ÿคซ  solution_10816

N, n = int(input()), list(map(int, input().split()))
M, m = int(input()), list(map(int, input().split()))

cnt = {}
for i in n:
    if i in cnt:
        cnt[i] += 1
    else:
        cnt[i] = 1

# ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
# print(cnt)  => {6: 1, 3: 2, 2: 1, 10: 3, -10: 2, 7: 1}

for i in m:
    if i in cnt:
        print(cnt[i], end = " ")
    else:
        print(0, end = " ")

 

 

๐Ÿง ๋ฐฑ์ค€ 11478

๐Ÿค”์‚ฌ์‹ค ๋ฌธ์ œ์—์„œ ๋งํ•˜๋Š” ๋ฐ”๋Š” ๋“ฑ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผ!

๐Ÿค” Algorithm ๊ณผ์ • 
1. s์— ๋ฐ”๋กœ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
์ด๋•Œ, s์— "abcde"๋ผ๋Š” ๋ฌธ์ž์—ด์ด ๋“ค์–ด์™”์„๋•Œ, s[1:3]์œผ๋กœ bc์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.]

2.  set_s = set()๋ฅผ ํ†ตํ•œ set์„ ์ƒ์„ฑํ•ด์ฃผ๊ณ 
3. ์ด์ค‘ for๋ฌธ์—์„œ ์œ„์˜ ์ธ๋ฑ์Šค ์Šฌ๋ผ์ด์‹ฑ์›๋ฆฌ๋ฅผ ์ด์šฉํ•ด ๋ถ€๋ถ„๋ฌธ์ž์—ด์„ ๊ตฌํ•œ๋‹ค.
4. ์ด๋•Œ, set_s์— ๋„ฃ์–ด์ฃผ๋Š”๋ฐ, ์ค‘๋ณต๋œ ๋ฌธ์ž์—ด์€ ์ถ”๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค.


๐Ÿคซ  solution_11478

s = input()
# print(s[1:3])  ba์ถœ๋ ฅ

set_s = set()

# ์ด์ค‘ for๋ฌธ์„ ๋Œ๋ฉด์„œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๊ตฌํ•˜๊ณ  set_s์—์ถ”๊ฐ€ํ•œ๋‹ค.
# ์ด๋•Œ, set_s๋Š” ์ง‘ํ•ฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต๋œ ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค.
for i in range(len(s)):
    for j in range(i, len(s)):
        set_s.add(s[i:j+1])

print(len(set_s))

+ Recent posts