ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

17413๋ฒˆ: ๋‹จ์–ด ๋’ค์ง‘๊ธฐ 2

๋ฌธ์ž์—ด S๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ๋ฌธ์ž์—ด์—์„œ ๋‹จ์–ด๋งŒ ๋’ค์ง‘์œผ๋ ค๊ณ  ํ•œ๋‹ค. ๋จผ์ €, ๋ฌธ์ž์—ด S๋Š” ์•„๋ž˜์™€๊ณผ ๊ฐ™์€ ๊ทœ์น™์„ ์ง€ํ‚จ๋‹ค. ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž('a'-'z'), ์ˆซ์ž('0'-'9'), ๊ณต๋ฐฑ(' '), ํŠน์ˆ˜ ๋ฌธ์ž('<', '>')๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 9039 ๋‹จ์–ด ๋’ค์ง‘๊ธฐ๋Š” ๋‹จ์ˆœํžˆ ๋’ค์ง‘๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์‹ฌํ”Œ ๊ทธ์ž์ฒด์ด๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์–ด ๋’ค์ง‘๊ธฐ 2๋Š” < >์˜ ์‚ฌ์ด์— ์žˆ๋Š” ํƒœ๊ทธ ๋ฌธ์ž์˜ ๊ฒฝ์šฐ ๋ฐ˜์ „์„ ํ•˜์ง€ ์•Š์œผ๋ฉฐ ๋‚˜๋จธ์ง€๋Š” ๋ฐ˜์ „์„ ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • reverse ํ”Œ๋ž˜๊ทธ๋ฅผ ํ†ตํ•ด ๋ฐ˜์ „ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•œ๋‹ค.
  • '<' ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด, ๋ฐ˜์ „์ด ๋ถˆํ•„์š” ํ•˜๋ฏ€๋กœ reverse = False๋กœ ์„ค์ •ํ•œ๋‹ค.
  • '>' ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๋ฐ˜์ „์ด ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ reverse = True๋กœ ์„ค์ •ํ•œ๋‹ค.
  • reverse์˜ ์ดˆ๊ธฐ๊ฐ’์€ True๋กœ ์„ค์ •ํ•˜์—ฌ '<' ๊ฐ€ ์ž…๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ฐ”๋กœ ๋ฐ˜์ „๋  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

 stack์œผ๋กœ ๋ถ„๋ฅ˜๋œ ๋ฌธ์ œ์ธ๋ฐ ์ด ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด stack์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค.  '<'๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด stack์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ๋„ ์žˆ์ง€๋งŒ, ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ์ฐพ๊ธฐ ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ๋ฒˆ ์ค‘์ฒฉ๋˜๋Š” ์ƒํ™ฉ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด stack์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜๋ฉฐ, stack์— push, pop์„ ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆํ•„์š”ํ•œ ์˜ค๋ฒ„ํ—ค๋“œ์ด๋‹ค.๐Ÿ˜

 

์ฝ”๋“œ

from sys import stdin


if __name__ == "__main__":
    s = stdin.readline().rstrip()
    reverse = True
    answer = ''
    word = ''
    for c in s:
        if c == '<':
            reverse = False
            answer += word
            word = '<'
        elif c == '>':
            reverse = True
            answer += (word + '>')
            word = ''
        elif c == ' ':
            answer += (word + ' ')
            word = ''
        elif reverse:
            word = c + word
        else:
            word += c

    print(answer + word)
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€