Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14-Hwangyerin #63

Merged
merged 2 commits into from
Feb 20, 2024
Merged

14-Hwangyerin #63

merged 2 commits into from
Feb 20, 2024

Conversation

Hwangyerin
Copy link
Collaborator

@Hwangyerin Hwangyerin commented Feb 15, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

Baekjoon | ํด๋ฆฌ์˜ค๋ฏธ๋…ธ

๋ฌธ์ œ
๋ฏผ์‹์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ 2๊ฐœ๋ฅผ ๋ฌดํ•œ๊ฐœ๋งŒํผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. AAAA์™€ BB

์ด์ œ '.'์™€ 'X'๋กœ ์ด๋ฃจ์–ด์ง„ ๋ณด๋“œํŒ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฏผ์‹์ด๋Š” ๊ฒน์นจ์—†์ด 'X'๋ฅผ ๋ชจ๋‘ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋กœ ๋ฎ์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ, '.'๋Š” ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋กœ ๋ฎ์œผ๋ฉด ์•ˆ ๋œ๋‹ค.

ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋กœ ๋ชจ๋‘ ๋ฎ์€ ๋ณด๋“œํŒ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ
์ฒซ์งธ ์ค„์— ๋ณด๋“œํŒ์ด ์ฃผ์–ด์ง„๋‹ค. ๋ณด๋“œํŒ์˜ ํฌ๊ธฐ๋Š” ์ตœ๋Œ€ 50์ด๋‹ค.

์ถœ๋ ฅ
์ฒซ์งธ ์ค„์— ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„œ๋Š” ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋ฎ์„ ์ˆ˜ ์—†์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋ž€? ํฌ๊ธฐ๊ฐ€ ์ „๋ถ€ ๋™์ผํ•œ ์ •์‚ฌ๊ฐํ˜•์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ด์–ด๋ถ™์—ฌ์„œ ๋งŒ๋“  ๋„ํ˜•์ด๋‹ค.
image

1์ฐจ ์‹œ๋„ - ์‹คํŒจ

โ–ผ์ ‘๊ทผ๋ฐฉ๋ฒ•

  1. '.'์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•œ๋‹ค.
board = input().split('.')
  1. '.'์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ–ˆ์„ ๋•Œ ์—ฐ์†๋œ '.'์€ ''(๋นˆ ๋ฌธ์ž์—ด)์œผ๋กœ ์ €์žฅ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ '.'์œผ๋กœ ๋˜๋Œ๋ฆฐ๋‹ค.
 if board[i] == '':
        board[i] = '.'
  1. 'X'๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ผ ๋•Œ ์ง์ˆ˜์ธ์ง€ ํ™€์ˆ˜์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
  2. ์ง์ˆ˜๋ผ๋ฉด X๋ฅผ ์ฃผ์–ด์ง„ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋กœ ๊ต์ฒด
    a. ํฐ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ(4)๋กœ ๋จผ์ € X๋ฅผ ์ฑ„์šฐ๊ณ  ์ดํ›„ ๋‚จ์€ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋กœ ์ฑ„์šฐ๊ธฐ(2)
  3. ํ™€์ˆ˜๋ผ๋ฉด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ธธ์ด๊ฐ€ 4, 2์ธ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋กœ ์ฑ„์šฐ์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด๋“œ๋ฅผ -1๋กœ ์ดˆ๊ธฐํ™”
  4. โš ๏ธboard๊ฐ€ -1๋ผ๋ฉด -1 ์ถœ๋ ฅ/ -1์ด ์•„๋‹ˆ๋ผ๋ฉด ์ถœ๋ ฅ ์‹œ ๋‹ค์‹œ ๋ฆฌ์ŠคํŠธ ์š”์†Œ๋“ค์„ '.'๋กœ ์—ฐ๊ฒฐ

๋‹ค์‹œ ๋ฆฌ์ŠคํŠธ๋ฅผ '.'์œผ๋กœ ์—ฐ๊ฒฐํ•˜๋ฉด์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค.
์ž…๋ ฅ ๊ฐ’์ด XX.XXXXXXXXXX..XXXXXX ์ผ ๊ฒฝ์šฐ

BB.AAAAAAAABB..AAAABB
์ฒ˜๋Ÿผ ๋‚˜์™€์•ผ ๋งž๋Š”๋ฐ
BB.AAAAAAAABB...AAAABB
์™€ ๊ฐ™์ด '.'์œผ๋กœ ์—ฐ๊ฒฐํ•˜๋ฉด์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค.('.'์ด ํ•˜๋‚˜ ๋” ๋ถ™์Œ)

['BB', 'AAAAAAAABB', '.', 'AAAABB']
'AAAAAAAABB', '.' , 'AAAABB'๋ฅผ '.'์œผ๋กœ ์—ฐ๊ฒฐํ•˜๊ฒŒ ๋˜๋ฉด์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜์ด๋‹ค.

board = input().split('.')
len_board = len(board)

for i in range(len_board):
    if board[i] == '':
        board[i] = '.'
    else:
        if len(board[i]) % 2 == 1:
            board = -1
            break
        else:
            # X๋ฅผ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋กœ ๊ต์ฒด
            num_A = len(board[i]) // 4
            num_B = (len(board[i]) % 4) // 2
            board[i] = 'A' * (num_A * 4) + 'B' * (num_B * 2)

# -1์ธ ๊ฒฝ์šฐ ์ถœ๋ ฅ
if board == -1:
    print(-1)
else:
    # ๋ณด๋“œ ์ถœ๋ ฅ
    print('.'.join(board))

2์ฐจ ์‹œ๋„ - ์„ฑ๊ณต

โ–ผ์ ‘๊ทผ๋ฐฉ๋ฒ•
๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค 0๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ 'AAAA','BB'๋กœ ์ฑ„์›Œ ๋„ฃ๋Š”๋‹ค.
์ฑ„์›Œ ๋„ฃ์—ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  X๊ฐ€ ๋‚จ๋Š”๋‹ค๋ฉด X์˜ ๊ฐœ์ˆ˜๋Š” ํ™€์ˆ˜์ด๋ฏ€๋กœ -1์„ ์ถœ๋ ฅ
์ด์™ธ 'X'๊ฐ€ ์•„๋‹Œ '.'์ผ ๊ฒฝ์šฐ์—๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋‹ค์Œ์œผ๋กœ ๋„˜๊ฒจ์ค€๋‹ค.(+1)

  1. ๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•„ ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•œ๋‹ค.
  2. ํ˜„์žฌ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ํ˜„์žฌ์ธ๋ฑ์Šค+4 ๊นŒ์ง€ ์š”์†Œ๋ฅผ ์กฐ์ธํ–ˆ์„ ๋•Œ 'XXXX'๋ผ๋ฉด ํ˜„์žฌ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 4๊ฐœ์˜ ๊ฐ’์„ 'A', 'A', 'A', 'A'๋กœ ๋ฐ”๊พผ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ์ธ๋ฑ์Šค์— 4๋ฅผ ๋”ํ•ด์ค€๋‹ค.
if ''.join(board[idx:idx+4]) == 'XXXX':
        board[idx:idx+4] = ['A', 'A', 'A', 'A']
        idx += 4
  1. elif ํ˜„์žฌ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ํ˜„์žฌ์ธ๋ฑ์Šค+2 ๊นŒ์ง€ ์š”์†Œ๋ฅผ ์กฐ์ธํ–ˆ์„ ๋•Œ 'XX'๋ผ๋ฉด ํ˜„์žฌ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 2๊ฐœ์˜ ๊ฐ’์„ 'B', 'B'๋กœ ๋ฐ”๊พผ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ์ธ๋ฑ์Šค์— 2๋ฅผ ๋”ํ•ด์ค€๋‹ค.
elif ''.join(board[idx:idx+2]) == 'XX':
        board[idx:idx+2] = ['B', 'B']
        idx += 2
  1. elif ํ˜„์žฌ ์ธ๋ฑ์Šค์˜ ๊ฐ’์ด 'X'์™€ ๊ฐ™๋‹ค๋ฉด ์ฆ‰, ํด๋ฆฌ์˜ค๋ฏธ๋…ธ 4, 2๋กœ ๋‹ค ์ฑ„์› ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  X ๊ฐ€ ๋‚จ์•˜๋‹ค๋ฉด(X๊ฐ€ ํ™€์ˆ˜๊ฐœ๋ผ๋ฉด)
    ๋ฆฌ์ŠคํŠธ๋ฅผ -1๋กœ ์ดˆ๊ธฐํ™”์‹œํ‚จ๋‹ค.
    elif board[idx] == 'X':
        board = -1
        break
  1. ํ˜„์žฌ ์š”์†Œ๊ฐ€ '.'์ผ ๋•Œ๋Š” ์ธ๋ฑ์Šค์— 1์„ ๋”ํ•ด์ค€๋‹ค.
  2. 2~5๋ฅผ ๋ฆฌ์ŠคํŠธ๊ธธ์ด๋ณด๋‹ค ์ž‘์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

๐Ÿ—๏ธ์ตœ์ข…์ฝ”๋“œ

board = list(input())
len_board = len(board)
idx = 0

while idx < len_board:
    if ''.join(board[idx:idx+4]) == 'XXXX':
        board[idx:idx+4] = ['A', 'A', 'A', 'A']
        idx += 4
    elif ''.join(board[idx:idx+2]) == 'XX':
        board[idx:idx+2] = ['B', 'B']
        idx += 2
    elif board[idx] == 'X':
        board = -1
        break
    else:
        idx += 1

if board == -1:
    print(board)
else:
    print(''.join(board))

๋˜ ๋‹ค๋ฅธ ํ’€์ด - replace()ํ•จ์ˆ˜ ํ™œ์šฉ

board = input()

board = board.replace("XXXX","AAAA") #board์— ์žˆ๋Š” ๋ชจ๋“  'XXXX'๋ฅผ 'AAAA'๋กœ ๊ต์ฒด
board = board.replace("XX","BB") #board์— ๋‚จ์•„ ์žˆ๋Š” ๋ชจ๋“  'XX'๋ฅผ 'BB'๋กœ ๊ต์ฒด

 #๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  'X'๊ฐ€ ๋‚จ์•„ ์žˆ๋‹ค๋ฉด -1์ถœ๋ ฅ ์•„๋‹ˆ๋ฉด ๋ฌธ์ž์—ด ์ถœ๋ ฅ
if 'X' in board:
    print(-1)
else:
    print(board)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

replace()ํ•จ์ˆ˜๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐ๋  ๋ฌธ์ œ๋ฅผ ๋น™๋น™ ๋Œ์•„ ํ‘ผ๊ฑด๊ฐ€ ์‹ถ๋‹ค.
๊ทธ๋ž˜๋„ ์ด๋Ÿฐ ์‹œํ–‰์ฐฉ์˜ค๋กœ ๋ฐœ์ „ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ฏฟ๋Š”๋‹ค!

Copy link
Collaborator

@LJEDD2 LJEDD2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด X๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ 4๊ฐœ, 2๊ฐœ์”ฉ ํ•œ๊บผ๋ฒˆ์— ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”์ฒœํ•ด๋“œ๋ฆฌ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ด๋ฏธ ํ•™์Šต์„ ์™„๋ฃŒํ•˜์…จ๊ตฐ์š” ใ…Žใ…Ž
์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ง๊ด€์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์–ด๋–ป๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์—ˆ๋Š”์ง€ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค !

s = input().replace('XXXX','AAAA').replace('XX','BB')
print(-1) if 'X' in s else print(s)

์ด๋ ‡๊ฒŒ ๋‹จ ๋‘ ์ค„๋งŒ์œผ๋กœ๋„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ .. โœจ
ํŒŒ์ด์ฌ์€ ์ •๋ง ๋Œ€๋‹จํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค

์ด๋ฒˆ์ฐจ์‹œ PR๋„ ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค !!

Copy link
Member

@avocado-13 avocado-13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ธ๋ฑ์Šค๋ž‘ joinํ•จ์ˆ˜ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•œ ๊ฒƒ๋„ ์‹ ๊ธฐํ–ˆ๋Š”๋ฐ replace ํ•จ์ˆ˜๋กœ๋„ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค.โœจ ์ด๋ฒˆ์ฐจ์‹œ PR๋„ ์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค! :)

@Hwangyerin Hwangyerin merged commit 46be201 into AlgoLeadMe:main Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants