๐ 17413 | ๋จ์ด ๋ค์ง๊ธฐ 2
https://www.acmicpc.net/problem/17413
๐ก Solution
Logic
1. '<>' ์์ ๋ค์ด ์๋ ๋ฌธ์๋ฅผ ์ ์ธํ๊ณ ๋์ด์ฐ๊ธฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋จ์ด๋ฅผ ๋ค์ง์ด์ผ ํ๋ค.
2. ๋ฐ๋ผ์ '<'๊ฐ ๋ค์ด์ค๋ฉด ์ง๊ธ๊น์ง ์์ธ word๋ฅผ ๋ค์ง์ด์ word_list์ pushํ๊ณ word๋ฅผ ์ด๊ธฐํํ๋ค. ๋ํ, skip์ด๋ผ๋ ๋ณ์๋ฅผ ๋ฌ์ ๊ดํธ๊ฐ ๋ซํ ๋๊น์ง ์ ๋ฐฉํฅ์ผ๋ก word๋ฅผ ์๋๋ค.
3. ์ดํ '>'๊ฐ ๋ค์ด์ค๋ฉด ์ง๊ธ๊น์ง ์์ word๋ฅผ word_list์ pushํ๊ณ skip ๋ณ์์ word ๋ณ์๋ฅผ ์ด๊ธฐํ ํ ๋ค ๋ค์ ๋ฌธ์๋ก ๋์ด๊ฐ๋ค.
4. '<>'์ ํด๋นํ์ง ์์ ๊ฒฝ์ฐ, ๋์ด์ฐ๊ธฐ๋ฅผ ๋ง์ฃผํ๊ธฐ ์ ๊น์ง word๋ฅผ ๋ ์๋๋ค.
5. ๋์ด์ฐ๊ธฐ๋ฅผ ๋ง์ฃผํ๋ฉด, ์ง๊ธ๊น์ง ์์์จ word๋ฅผ ๋ค์ง์ด์ word_list์ pushํ๊ณ ๋์ด์ฐ๊ธฐ๋ push ํด์ค๋ค. ์ดํ word ๋ณ์๋ฅผ ์ด๊ธฐํ ํด์ค ๋ค ๋์ด๊ฐ๋ค.
6. ๋ฌธ์ฅ์ ๋์ ๋๋ฌํ๋ฉด ๋ง์ฐฌ๊ฐ์ง๋ก ์ง๊ธ๊น์ง ์์์จ word๋ฅผ ๋ค์ง์ด์ word_list์ pushํ๋ค.
sentence = input()
skip = 0
word = ''
word_list = []
for i in range(len(sentence)) :
if sentence[i] == '<' :
word_list.append(word[::-1])
word = ''
word += sentence[i]
skip = 1
continue
if skip and sentence[i] == '>' :
word += sentence[i]
word_list.append(word)
word = ''
skip = 0
continue
if not skip :
if sentence[i] == ' ' :
word_list.append(word[::-1])
word_list.append(' ')
word = ''
continue
elif i == len(sentence) - 1 :
word += sentence[i]
word_list.append(word[::-1])
continue
word += sentence[i]
print(''.join(word_list))
โถ ๐ Note
โ ์ค๋ช
str1.find(substring, start, end)์ ์ฌ์ฉํ๋ฉด, <>
์ธ๋ฑ์ค๋ฅผ ๋น ๋ฅด๊ฒ ์ฐพ์
๊ฑด๋๋ฐ๋ ๊ณผ์ ์ ์ต์ ํํ ์ ์์ต๋๋ค.
๋ํ, Python์ yield
๋ฅผ ํ์ฉํ๋ฉด ์ํ๋ฅผ ์ ์งํ๋ฉด์ ์ฌ๋ฌ ๊ฐ์ ๋ฐํํ ์ ์์ด
ํจ์จ์ ์
๋๋ค. ''.join()
ํจ์๋ ๋ฐ๋ณต์ ์ผ๋ก ์ ๋๋ ์ดํฐ๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค.
โ yield - ๊ฐ์ ๋ฐํํ๋ฉด์ ํจ์์ ์ํ๋ฅผ ์ ์งํจ - ๋ฐ๋ณต(iteration)์ ํตํด ์ฌ๋ฌ ๊ฐ์ ๋ฐํ - ํจ์ ํธ์ถ ์ ๋งค๋ฒ ์คํ์ ์ด์ด์ ์งํ
'์๊ณ ๋ฆฌ์ฆ ๐ฉ๐ปโ๐ป > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BAEKJOON] 1935 | ํ์ ํ๊ธฐ์ 2 (0) | 2024.11.22 |
---|---|
[BAEKJOON] 17299 | ์ค๋ฑํฐ์ (0) | 2024.11.21 |
[BAEKJOON] 10799 | ์ ๋ง๋๊ธฐ (0) | 2024.11.20 |
[BAEKJOON] 17298 | ์คํฐ์ (0) | 2024.11.20 |
[BAEKJOON] 1406 | ์๋ํฐ (0) | 2024.11.19 |