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

16-janghw0126 #183

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

16-janghw0126 #183

wants to merge 11 commits into from

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Oct 30, 2024

πŸ”— 문제 링크

λ°±μ€€ | λ¬Έμžμ—΄ 폭발

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

50λΆ„

✨ μˆ˜λ„ μ½”λ“œ

πŸ‘‰λ¬Έμ œ μ½”λ“œ λ°”λ‘œκ°€κΈ°

이 λ¬Έμ œλŠ” μŠ€νƒμ„ μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” μ•„μ΄λ””μ–΄λ§Œ νŒŒμ•…ν•˜λ©΄ μ‰½κ²Œ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
그치만 λ¬Έμžμ—΄μ„ μž…λ ₯λ°›μ•„μ•Ό 되기 λ•Œλ¬Έμ— 이λ₯Ό μ²˜λ¦¬ν•˜λŠ” λΆ€λΆ„μ—μ„œ 쑰금 κΉŒλ‹€λ‘œμ› μŠ΅λ‹ˆλ‹€.

λ¬Έμ œμ—μ„œλŠ” λ¬Έμžμ—΄μ— 폭발 λ¬Έμžμ—΄μ„ μ‹¬μ–΄λ†“μ•˜κ³ , 폭발 λ¬Έμžμ—΄μ„ ν¬ν•¨ν•˜κ³  μžˆλŠ” κ²½μš°μ— ν•΄λ‹Ή λ¬Έμžμ—΄μ„ μ—†μ•€ ν›„ 남은 λ¬Έμžμ—΄μ„ μˆœμ„œλŒ€λ‘œ 이어 λΆ™μ—¬ μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ„ λ§Œλ“ λ‹€κ³  ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ μ €λŠ” μŠ€νƒμ— μ°¨λ‘€λŒ€λ‘œ 문자 ν•˜λ‚˜ν•˜λ‚˜λ₯Ό λ„£μœΌλ©΄μ„œ 폭발 λ¬Έμžμ—΄κ³Ό μŠ€νƒμ— μžˆλŠ” λ¬Έμžμ—΄μ„ λΉ„κ΅ν•˜μ—¬ 폭발 λ¬Έμžμ—΄μ΄ μžˆλŠ”μ§€ ν™•μΈν•˜κ³  μžˆλŠ” 경우 pop()을 ν•˜λ©΄ λ˜κ² λ‹€κ³  μƒκ°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

πŸ”Ž 문제 둜직

  1. ν•΄λ‹Ή ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€λ₯Ό μž…λ ₯ λ°›μŠ΅λ‹ˆλ‹€.
  2. 폭발 λ¬Έμžμ—΄μ˜ 길이λ₯Ό κ³„μ‚°ν•©λ‹ˆλ‹€.
# λ¬Έμžμ—΄ μž…λ ₯ν•˜κΈ°
string = input().strip()
# 폭발 λ¬Έμžμ—΄ μž…λ ₯ν•˜κΈ°
bomb_string = input().strip()

# 폭발 λ¬Έμžμ—΄ 길이 κ³„μ‚°ν•˜κΈ°
bomb_len=len(bomb_string)
  1. μž…λ ₯받은 문자λ₯Ό ν•˜λ‚˜ν•˜λ‚˜ μˆœνšŒν•˜λ©΄μ„œ stack에 λ„£μŠ΅λ‹ˆλ‹€.
stack.append(str)
  1. stack에 μŒ“μ•„λ‘λ‹€κ°€ 폭발 λ¬Έμžμ—΄μ˜ 길이만큼 stack의 뒷뢀뢄이 폭발 λ¬Έμžμ—΄κ³Ό 같은 경우 ν•΄λ‹Ή λ¬Έμžμ—΄μ„ pop ν•΄μ€λ‹ˆλ‹€.
# 폭발 λ¬Έμžμ—΄κ³Ό stack 뒷뢀뢄이 같은 경우
    if ''.join(stack[-bomb_len:])==bomb_string:
        # ν•΄λ‹Ή λ¬Έμžμ—΄ pop
        for _ in range(bomb_len):
            stack.pop()
  1. λ‚¨μ•„μžˆλŠ” λ¬Έμžκ°€ μ—†λŠ” 경우 "FRULA"λ₯Ό 좜λ ₯ν•˜κ³  μ•„λ‹ˆλ©΄ λ¬Έμžμ—΄μ„ 좜λ ₯ν•©λ‹ˆλ‹€.
# κ²°κ³Ό 좜λ ₯
print(''.join(stack) if stack else "FRULA",end='')

πŸ‘©β€πŸ’»μ΅œμ’… μ½”λ“œ

import sys
input=sys.stdin.readline

# λ¬Έμžμ—΄ μž…λ ₯ν•˜κΈ°
string = input().strip()
# 폭발 λ¬Έμžμ—΄ μž…λ ₯ν•˜κΈ°
bomb_string = input().strip()

# 폭발 λ¬Έμžμ—΄ 길이 κ³„μ‚°ν•˜κΈ°
bomb_len=len(bomb_string)

# κ²°κ³Ό 좜λ ₯ 리슀트 μ„ μ–Έ
stack=[]

# stack에 μŒ“μ•„λ‘κ³ , ν­λ°œλ¬Έμžμ—΄μ΄ μžˆλŠ” 경우 pop
for str in string:
    stack.append(str)
    # 폭발 λ¬Έμžμ—΄κ³Ό stack 뒷뢀뢄이 같은 경우
    if ''.join(stack[-bomb_len:])==bomb_string:
        # ν•΄λ‹Ή λ¬Έμžμ—΄ pop
        for _ in range(bomb_len):
            stack.pop()

# κ²°κ³Ό 좜λ ₯
print(''.join(stack) if stack else "FRULA",end='')

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

μ‹œν—˜ 기간이 λ“œλŽŒ λλ‚¬μ”λ‹ˆλ‹€,,,πŸ˜‚
μ‹œν—˜ 곡뢀λ₯Ό ν•˜λŠλΌ μ•Œκ³ λ¦¬μ¦˜ 감을 살짝 μžƒμ–΄λ²„λ €μ„œ μ›Œλ°μ—… κ²Έ κ΅¬ν˜„ 문제λ₯Ό ν’€μ–΄λ³΄μ•˜λŠ”λ° 아직 뢀쑱함을 λŠλ‚λ‹ˆλ‹€..γ… 
PR이 λ°€λ¦°λ§ŒνΌ μ—΄μ‹¬νžˆ λ‹¬λ €λ³΄κ² μŠ΅λ‹ˆλ‹€!

Copy link
Member

@jung0115 jung0115 left a comment

Choose a reason for hiding this comment

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

μ €λŠ” μ²˜μŒμ— λ‹¨μˆœν•˜κ²Œ 폭탄 λ¬Έμžμ—΄μ΄ 없을 λ•ŒκΉŒμ§€ replace둜 μ§€μ›Œμ£Όλ©΄ λ˜μ§€ μ•Šμ„κΉŒ? ν•˜κ³  μ•„λž˜μ²˜λŸΌ μ½”λ“œλ₯Ό μž‘μ„±ν–ˆλ”λ‹ˆ λ°”λ‘œ λ©”λͺ¨λ¦¬ μ΄ˆκ³Όκ°€ λ‚¬μŠ΅λ‹ˆλ‹€,,,

while(str.indexOf(bomb) != -1) {
  str = str.replace(bomb, "")
}

κ·Έλž˜μ„œ 저도 μŠ€νƒμ„ 써볼까 ν•˜λ‹€κ°€ gptν•œν…Œ μŠ€νƒ μ•ˆ μ“°κ³  ν•΄κ²°ν•  수 μžˆλŠ” 방법을 μ•Œλ €λ‹¬λΌκ³  ν–ˆλ”λ‹ˆ μ•„λž˜μ™€ 같은 μ½”λ“œλ₯Ό μ•Œλ €μ€¬μ–΄μš”!
ν•˜μ§€λ§Œ μ½”λ“œ λ‚΄μš©μ„ λœ―μ–΄λ³΄λ©΄ 사싀상 Stack μžλ£Œν˜•μ„ λ°”λ‘œ 쓰지 μ•Šμ•˜λ‹€λŠ” 것 뿐이지 λ™μž‘ μ›λ¦¬λŠ” Stackκ³Ό λ™μΌν•˜κ³ , ν˜œμ› λ‹˜ ν’€μ΄λž‘λ„ 같은 κ±°λ”λΌκ΅¬μš” πŸ€”

fun main() {
  val br = BufferedReader(InputStreamReader(System.`in`))

  var str = br.readLine()
  val bomb = br.readLine()
  val bombLen = bomb.length
  val result = StringBuilder()

  for (char in str) {
    result.append(char)

    if (result.endsWith(bomb)) {
      result.delete(result.length - bombLen, result.length)
    }
  }

  if (result.isEmpty()) {
    println("FRULA")
  } else {
    println(result.toString())
  }
}

fun StringBuilder.endsWith(suffix: String): Boolean {
  if (this.length < suffix.length) return false
  return this.substring(this.length - suffix.length) == suffix
}

이번 μ°¨μ‹œλ„ μˆ˜κ³ ν•˜μ…¨μ–΄μš”!

Comment on lines +16 to +22
for str in string:
stack.append(str)
# 폭발 λ¬Έμžμ—΄κ³Ό stack 뒷뢀뢄이 같은 경우
if ''.join(stack[-bomb_len:])==bomb_string:
# ν•΄λ‹Ή λ¬Έμžμ—΄ pop
for _ in range(bomb_len):
stack.pop()
Copy link
Collaborator

Choose a reason for hiding this comment

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

''.join()을 μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ λΉ„κ΅ν•˜λŠ” 뢀뢄을 μ΅œμ ν™”ν•˜μ—¬ μ‹œκ°„μ„ 쑰금 더 μ€„μ—¬λ³΄μ•˜μŠ΅λ‹ˆλ‹€ !

  • 폭발 λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰ 문자λ₯Ό λ¨Όμ € ν™•μΈν•˜μ—¬ λΆˆν•„μš”ν•œ λ¬Έμžμ—΄ 비ꡐλ₯Ό μ€„μ˜€μŠ΅λ‹ˆλ‹€.
  • ''.join() ν˜ΈμΆœμ„ μ΅œμ†Œν™”ν–ˆμŠ΅λ‹ˆλ‹€.
for char in string:
    stack.append(char)
    
    # ν˜„μž¬ λ¬Έμžκ°€ 폭발 λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰ λ¬Έμžμ™€ κ°™κ³ 
    # μŠ€νƒμ˜ 길이가 폭발 λ¬Έμžμ—΄ 이상일 λ•Œλ§Œ 검사
    if char == last_char and len(stack) >= bomb_len:
        # 폭발 λ¬Έμžμ—΄κ³Ό μΌμΉ˜ν•˜λŠ”μ§€ 직접 비ꡐ
        is_bomb = True
        for i in range(bomb_len):
            if stack[-(bomb_len-i)] != bomb[i]:
                is_bomb = False
                break
        
        if is_bomb:
            del stack[-bomb_len:]  # pop() λŒ€μ‹  del μ‚¬μš©ν•˜μ—¬ ν•œλ²ˆμ— 제거

print(''.join(stack) if stack else "FRULA", end='')

μ›λž˜ μ½”λ“œλ³΄λ‹€ 쑰금 더 μ‹œκ°„μ΄ 빨라진 것을 확인할 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€ !
image

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.

3 participants