[BOJ][๐ŸŸก4][๋ฐฑ์ค€#09935] ๋ฌธ์ž์—ด ํญ๋ฐœ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #9935


๋ฌธ์ œ

์ƒ๊ทผ์ด๋Š” ๋ฌธ์ž์—ด์— ํญ๋ฐœ ๋ฌธ์ž์—ด์„ ์‹ฌ์–ด ๋†“์•˜๋‹ค. ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ํญ๋ฐœํ•˜๋ฉด ๊ทธ ๋ฌธ์ž๋Š” ๋ฌธ์ž์—ด์—์„œ ์‚ฌ๋ผ์ง€๋ฉฐ, ๋‚จ์€ ๋ฌธ์ž์—ด์€ ํ•ฉ์ณ์ง€๊ฒŒ ๋œ๋‹ค. ํญ๋ฐœ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค.

๋ฌธ์ž์—ด์ด ํญ๋ฐœ ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์—, ๋ชจ๋“  ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ํญ๋ฐœํ•˜๊ฒŒ ๋œ๋‹ค. ๋‚จ์€ ๋ฌธ์ž์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ์ด์–ด ๋ถ™์—ฌ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋งŒ๋“ ๋‹ค. ์ƒˆ๋กœ ์ƒ๊ธด ๋ฌธ์ž์—ด์— ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ํญ๋ฐœ์€ ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ๋ฌธ์ž์—ด์— ์—†์„ ๋•Œ๊นŒ์ง€ ๊ณ„์†๋œ๋‹ค.

์ƒ๊ทผ์ด๋Š” ๋ชจ๋“  ํญ๋ฐœ์ด ๋๋‚œ ํ›„์— ์–ด๋–ค ๋ฌธ์ž์—ด์ด ๋‚จ๋Š”์ง€ ๊ตฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๋‚จ์•„์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋•Œ๋Š” โ€œFRULAโ€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ํญ๋ฐœ ๋ฌธ์ž์—ด์€ ๊ฐ™์€ ๋ฌธ์ž๋ฅผ ๋‘ ๊ฐœ ์ด์ƒ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘˜์งธ ์ค„์— ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 36๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘ ๋ฌธ์ž์—ด์€ ๋ชจ๋‘ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž, ์ˆซ์ž 0, 1, โ€ฆ, 9๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ชจ๋“  ํญ๋ฐœ์ด ๋๋‚œ ํ›„ ๋‚จ์€ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

mirkovC4nizCC44
C4


์ถœ๋ ฅ

mirkovniz


์˜ˆ์ œ 2

์ž…๋ ฅ

12ab112ab2ab
12ab


์ถœ๋ ฅ

FRULA


My Sol

import sys
input = sys.stdin.readline

def check(idx):
    global part, pl
    stack = stacks[idx]

    if len(stack) != pl: return 0

    for i in range(pl):
        if not part[i] == stack[i]: return 0
    return 1


txt = input().rstrip()
part = input().rstrip()
pl = len(part)
stacks = [[]]
ls = [0]

cur_stack_idx = 0
k = 0
for t in txt:
    if t == part[k]:
        stacks.append([t])
        cur_stack_idx += 1
        ls.append(1)

    else:
        stacks[cur_stack_idx].append(t)
        ls[cur_stack_idx] += 1

    # ํ˜„์žฌ stack์— ๋ชจ์ธ ๋ฌธ์ž๊ฐ€ pl๊ณผ ๊ฐ™์„ ๋•Œ
    if ls[cur_stack_idx] == pl:
        while check(cur_stack_idx):
            stacks.pop()
            ls.pop()
            cur_stack_idx -= 1


def is_empty(stacks):
    for stack in stacks:
        if stack: return 0
    return 1

if is_empty(stacks):
    print('FRULA')
    quit()

for stack in stacks:
    print(*stack, sep='', end='')

์Šคํƒ์„ ์ด์šฉํ•ด ํ‘ธ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

  1. stack์„ ๋ชจ์•„๋†“์€ stacks ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  2. ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฌธ์ž๋ฅผ ๋ฐœ๊ฒฌํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ stack์— ์ถ”๊ฐ€
  3. stack์˜ ๊ธธ์ด๊ฐ€ part์˜ ๊ธธ์ด์™€ ๊ฐ™์•„์ง€๋ฉด ๊ฐ™์€์ง€ ์ฒดํฌํ•˜๋Š” ํ•จ์ˆ˜ ์‹คํ–‰
  4. ๋งŒ์•ฝ ๊ฐ™๋‹ค๋ฉด stack์„ stacks์—์„œ ์ œ๊ฑฐ
  5. ์ƒˆ๋กœ์šด t๊ฐ€ ์ด์ „ stack์— ๊ณ„์† ์ถ”๊ฐ€๋˜์–ด pl๊ณผ ๊ฐ™์•„์ง€๋ฉด ๋‹ค์‹œ check
  6. ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณต
  7. ๋‚จ์€ stacks๋ฅผ ๊ณต๋ฐฑ ๊ฐ„๊ฒฉ ์—†์ด ์ถœ๋ ฅ
  8. ๋งŒ์•ฝ stacks๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด FRULA ์ถœ๋ ฅ


๋ฆฌ์ŠคํŠธ ๋‚ด๋ถ€์— ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•˜๋‚˜ ์žˆ์œผ๋ฉด False๊ฐ€ ์•„๋‹Œ True์ธ ๊ฒƒ์— ์ฃผ์˜ํ•ด์•ผ ํ–ˆ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

์ฒ˜์Œ์—๋Š” list๋ฅผ ์ด์šฉํ•ด ํญ๋ฐœ ๋ฌธ์ž์—ด์„ ๋ฐœ๊ฒฌํ•˜๋ฉด ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒํผ popํ•˜์—ฌ ์ œ๊ฑฐํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ–ˆ๋Š”๋ฐ, ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„๋ฅ˜๋ฅผ ์ฐธ๊ณ ํ•˜์˜€๋‹ค.


๋ชจ๋ฒ”๋‹ต์•ˆ

def main():
 
    string = input()    # ์ „์ฒด ๋ฌธ์ž์—ด
    bomb = input()      # ํญ๋ฐœ ๋ฌธ์ž์—ด
 
    lastChar = bomb[-1] # ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ธ€์ž
    stack = []
    length = len(bomb)  # ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด
 
    for char in string:
        stack.append(char)
        if char == lastChar and ''.join(stack[-length:]) == bomb:
            del stack[-length:]
 
    answer = ''.join(stack)
 
    if answer == '':
        print("FRULA")
    else:
        print(answer)

๋‚ด ํ’€์ด์— ๋น„ํ•ด ์ฝ”๋“œ๊ธธ์ด๋Š” ์•ฝ 45%, ์—ฐ์‚ฐ์‹œ๊ฐ„์€ ์•ฝ 25% ์ •๋„์ธ ํ’€์ด(์ถœ์ฒ˜)๋ฅผ ๋ฐœ๊ฒฌํ•ด ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค.


์ด ๋ถ„์˜ ์ ‘๊ทผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์ž…๋ ฅ๋ฌธ์ž์—ด์„ ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€์ฐจ๋ก€ ํ•œ ๊ธ€์ž์”ฉ ์Šคํƒ์— push ํ•ฉ๋‹ˆ๋‹ค.

  2. ํ˜„์žฌ ๊ธ€์ž๊ฐ€ ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ธ€์ž์™€ ์ผ์น˜ํ•˜๋ฉด ์Šคํƒ์˜ top๋ถ€ํ„ฐ ํญ๋ฐœ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊นŒ์ง€ ํ™•์ธํ•˜์—ฌ ํญ๋ฐœ๋ฌธ์ž์—ด์ด ๋งŒ๋“ค์–ด์ง€๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

  3. ํญ๋ฐœ๋ฌธ์ž์—ด์ด ๋งŒ๋“ค์–ด์ง„๋‹ค๋ฉด ๋งŒ๋“ค์–ด์ง€๋Š” ํญ๋ฐœ๋ฌธ์ž์—ด์„ ์Šคํƒ์—์„œ popํ•ฉ๋‹ˆ๋‹ค.

  4. 1~3์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

  5. ๋ฌธ์ž์—ด ์ˆœํšŒ๋ฅผ ๋งˆ์น˜๊ณ  ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด, FRULA๋ฅผ ์ถœ๋ ฅ, ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ์Šคํƒ ์† ๋ฌธ์ž์—ด์„ ์ฐจ๋ก€๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.


๋’ค์—์„œ๋ถ€ํ„ฐ ํ™•์ธํ•˜๋ฉด ๋  ์ผ์ด์—ˆ๋‹ค. ๊ต‰์žฅํžˆ ๋˜‘๋˜‘ํ•œ ํ’€์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๋‚˜์ฒ˜๋Ÿผ ๋‹ค์ค‘ stack์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ•˜๋‚˜์˜ stack์œผ๋กœ๋งŒ ํ•ด๊ฒฐํ•œ ๊ฒƒ์ด๋‹ˆ ๋ง์ด๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด stacks๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜ ์—ญ์‹œ ํ•„์š”๊ฐ€ ์—†๋‹ค.

๋‹ค๋งŒ answer์— ๋‹ต์„ ๋ชจ์•„์„œ ์ถœ๋ ฅํ–ˆ๋Š”๋ฐ, ๋‚˜๋Š” ๋ณ„๋„์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์—†์ด ๋ฐ”๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด is_empty ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์ด ๋ถ„์€ if stack: ์œผ๋กœ FRULA๋ฅผ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ์ถœ๋ ฅ ํ˜•์‹์— ๋”ฐ๋ผ ๋ฐ”๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ ํ•ด๋‹น ๋ถ€๋ถ„์€ ์•„์‰ฝ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ์ „์ฒด์ ์œผ๋กœ ๋„ˆ๋ฌด๋‚˜ ํ›Œ๋ฅญํ•œ ์ ‘๊ทผ๊ณผ ํ’€์ด์˜€๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ