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

15-H0ngJu #188

Merged
merged 1 commit into from
May 17, 2024
Merged

15-H0ngJu #188

merged 1 commit into from
May 17, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented May 14, 2024

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

A์™€ B

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

2H

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

๊ทธ๋ฆฌ๋”” ์—ฐ์Šตํ•˜๋ ค๊ณ  ์ด ๋ฌธ์ œ๋ฅผ ๊ณจ๋ž์Šต๋‹ˆ๋‹ท ~!

๋ฌธ์ œ ์š”์•ฝ
์•„๋ž˜์˜ ๊ทœ์น™์— ๋”ฐ๋ผ S๋ฅผ T๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉด 1์„, ์—†์œผ๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•˜๋ผ@!
๊ทœ์น™ 1. ๋ฌธ์ž์—ด ๋’ค์— A๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
๊ทœ์น™ 2. ๋ฌธ์ž์—ด์„ ๋’ค์ง‘๊ณ  ๋’ค์— B๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.



์ฒ˜์Œ์—๋Š” ๋ฌธ์ œ ์กฐ๊ฑด๋Œ€๋กœ S๋ฅผ T๋กœ ๋งŒ๋“ค๋ ค๊ณ  ์ ‘๊ทผ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทผ๋ฐ, ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งŒ ๋ณด์•„๋„,
S๋ฅผ T๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด๊ณผ ๋กœ์ง์„ ์ฐพ๋Š”๊ฒŒ ์ง„์งœ ๋งŒ๋งŒ์น˜ ์•Š์•˜์Šต๋‹ˆ๋‹ค..

๊พธ์—ญ๊พธ์—ญ ๊ตฌํ˜„ํ•œ๋‹ค๋Š” ์ƒ๊ฐ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์•˜์ง€๋งŒ,
์žฅ๋ ฌํžˆ ์ „์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค... ๐Ÿชฆ๐Ÿชฆ

์‚ฌ์‹ค ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” ์–ด๋–ค ๋กœ์ง์ด ๋ฌธ์ œ์ธ์ง€ ํŒŒ์•…์ด ์•ˆ๋ผ์„œ ๊ทธ๋ƒฅ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค..

ํ•ญ์ƒ PR ์ž‘์„ฑํ•  ๋•Œ ์ฏค ๋˜๋ฉด ์™œ ๋‹ต์ด ์•ˆ๋‚˜์™”๋Š”์ง€, ์–ด๋–ค ๋กœ์ง์ด ํ‹€๋ ธ๋Š”์ง€ ๋ˆˆ์— ๋ณด์ด๋Š”๊ฑธ๊นŒ์š” .. ใ… ใ…œ

๋ˆ„๋”๊ธฐ ์ฝ”๋“œ
import sys

def input(): return sys.stdin.readline().rstrip()

S = list(input())
T = list(input())

def solution(cur, target, idx):
    print(cur, target, idx)
    if len(cur) == len(target): # ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ ๊ฐ™์•„์ง€๋ฉด ๋ฌด์กฐ๊ฑด ๋‹ต์ด๊ฑฐ๋‚˜, ์•„๋‹ˆ๊ฑฐ๋‚˜ ๋‘˜ ์ค‘ ํ•˜๋‚˜
        if cur == target:
            return 1
        else: return 0
    
    elif len(cur) < len(target): # ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ target๋ณด๋‹ค ์ž‘์„ ๋•Œ๋งŒ ์‹คํ–‰ํ•˜๋ฉด ๋จ
        if idx+1 < len(target): 
            if target[idx+1] == "B":
                cur.reverse()
                cur.append("B")
                idx += 1
                return solution(cur, target, idx)
            elif target[idx+1] == "A":
                cur.append("A")
                idx += 1
                return solution(cur, target, idx)
            else:
                return 0
        else:
            if target[idx] == "B":
                cur.reverse()
                cur.append("B")
                return solution(cur, target, idx)
            elif target[idx] == "A":
                cur.append("A")
                return solution(cur, target, idx)
            else:
                return 0

    else:
        return 0

print(solution(S, T, len(S)))



์ผ๋‹จ ์ฐพ์•„๋ณธ ๋ ˆํผ๋Ÿฐ์Šค์—์„œ ์•„๋ž˜์˜ ํžŒํŠธ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค.

'S์—์„œ T๋ฅผ ์ถ”์ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค, T์—์„œ S๋ฅผ ์—ญ์ถ”์  ํ•˜๋Š”๊ฒŒ ๋” ํšจ์œจ์ ์ด๋‹ค!'


๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ ์™„์„ฑ๋œ ์ตœ์ข… ์ˆ˜๋„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹น

  1. S์˜ ๊ธธ์ด์™€ T์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์•„์งˆ ๋•Œ๊นŒ์ง€ ์•„๋ž˜๋ฅผ ์ˆ˜ํ–‰
    1. T์˜ ๋งจ๋’ค ๊ธ€์ž๊ฐ€ A์ด๋ฉด ๊ทœ์น™ 1์— ์˜ํ•ด ์ƒ์„ฑ๋˜์—ˆ์œผ๋ฏ€๋กœ, T์—์„œ popํ•˜๊ณ  ๋‹ค์‹œ ์žฌ๊ท€
    2. T์˜ ๋งจ๋’ค ๊ธ€์ž๊ฐ€ B์ด๋ฉด ๊ทœ์น™ 2์— ์˜ํ•ด ์ƒ์„ฑ๋˜์—ˆ์œผ๋ฏ€๋กœ, T์—์„œ popํ•˜๊ณ  reverse๋ฅผ ํ•ด์ค€ ๋’ค ๋‹ค์‹œ ์žฌ๊ท€
  2. S์˜ ๊ธธ์ด์™€ T์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์•„์ง€๋ฉด S == T์ธ์ง€ ํ™•์ธ
    1. S == T์ด๋ฉด return 1
    2. S != T์ด๋ฉด return 0
  3. ์ดˆ๊ธฐ๊ฐ’ S๊ฐ€ T๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ(๋ฌธ์ œ ์„ฑ๋ฆฝ X) ์ผ€์ด์Šค์— ๋Œ€ํ•˜์—ฌ return 0

.
.
.

๊ทธ๋Ÿฐ๋ฐ ๊ณ„์†ํ•ด์„œ ๋Ÿฐํƒ€์ž„์—๋Ÿฌ(RecursionError) ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๊ณ ์š” ...

ํ˜น์‹œ ๊ณ„์†ํ•ด์„œ ์žฌ๊ท€๋˜๋Š” ์ผ€์ด์Šค๊ฐ€ ์žˆ๋‚˜ ์‹ถ์–ด์„œ ์งˆ๋ฌธ๊ฒŒ์‹œํŒ ์˜ˆ์‹œ๋“ค๋„ ๋‹ค ๋Œ๋ ค๋ดค์œผ๋‚˜ .. ํ•ด๊ฒฐ์„ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค



๊ทธ๋Ÿฌ๋‹ค๊ฐ€

๋™๋ฐฉ์— ์žˆ๋Š” @9kyo-hwang ๋„์›€์œผ๋กœ ..

sys.setrecursionlimit(10**6)

recursion limit์„ ๋Š˜๋ ธ๋”๋‹ˆ .. ํ†ต๊ณผ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ... ๐Ÿฅน๐Ÿฅน

ํ›„ ์˜ˆ์ „์— ๋ชป ํ’€์—ˆ๋˜ ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ๋“ค๋„ ๋„์žฅ๊นจ๊ธฐ ๊ฐ‘๋‹ˆ๋‹ท ๐Ÿ‘Š๐Ÿ‘Š๐Ÿ‘Š๐Ÿ‘Š๐Ÿ‘Š

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

ํŒŒ์ด์ฌ recursion error๋Š” sys.setrecursionlimit(106)๋กœ ํ•ด๊ฒฐํ•˜๊ธฐ ..
์ด๊ฑฐ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ํ•œ ํ•œ์‹œ๊ฐ„์€ ์ณ๋‹ค ๋ณธ ๊ฒƒ ๊ฐ™๋„ค์š” ๐Ÿซ 

image

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

image

์™€ ์ €๋„ ์ด ๋ฌธ์ œ S -> T๋กœ ํ’€๋ ค๋‹ค๊ฐ€ gg ์น˜๊ณ  ๋ ˆํผ๋Ÿฐ์Šค ๋ณด๊ณ  T -> S๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค..

์ฝ”ํ…Œ์— ๋‚˜์™”์œผ๋ฉด ํ‹€๋ ธ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.....................

์™€ํ์ฃผ ์งฑ..........




import sys

def input(): return sys.stdin.readline().rstrip()

S = list(input())
T = list(input())

while len(T) > len(S):
    if T[-1] == "A": T.pop()
    else:
        T.pop()
        T = T[::-1]

if S == T: print(1)
else: print(0)

Comment on lines +9 to +27
def solution(cur, target):
if len(cur) == len(target): # len(cur) == len(target)
if cur == target:
return 1
else: return 0

elif len(cur) < len(target): # len(cur) < len(target)
if target[-1] == "A":
target.pop()
return solution(cur, target)
else:
target.pop()
target = target[::-1]
return solution(cur, target)

else: # len(cur) > len(target)
return 0

print(solution(S, T))
Copy link
Member

Choose a reason for hiding this comment

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

๊ทผ๋ฐ ์ด ๋ถ€๋ถ„ ์žฌ๊ท€๋กœ ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?

๋˜‘๊ฐ™์€ ๊ตฌํ˜„์ด๋ฉด ํŒŒ์ด์ฌ์€ ์žฌ๊ท€ํƒ€๋Š”๊ฒŒ ๋ฌด๊ฑฐ์›Œ์„œ while๋ฌธ์ด ํ›จ์”ฌ ๋นจ๋ผ์š”!!!

์ €๋ ‡๊ฒŒ recursionlimit ์„ค์ •ํ•ด์ค„ ํ•„์š”๋„ ์—†๊พธ์š” ํ•˜ํ•˜




์จ‹๋“  ์จ‹๋“ .... ์ข‹์€ ๋ฌธ์ œ ๋„ˆ๋ฌด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค์š”์ž‡

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์‚ฌ์‹ค ๋ณ„๋‹ค๋ฅธ ์ด์œ ๋Š” ์—†์Šต๋‹ˆ๋‹ค.. ํ—ˆํ—ˆ
์ฒ˜์Œ์— ํ•จ์ˆ˜ ์„ค๊ณ„ํ•  ๋•Œ '์žฌ๊ท€ ์จ์•ผ์ง€~' ํ•ด์„œ ์ž‘์„ฑํ•˜๋‹ค๋ณด๋‹ˆ ์ €๋ ‡๊ฒŒ ๋œ ๊ฒ๋‹ˆ๋‹ค์š”... ใ…‹.ใ…‹.ใ…‹.

ํƒœ๊ทœ๋‹˜ ๋ง๋Œ€๋กœ ์‚ฌ์‹ค ์ € ์žฌ๊ท€๋กœ์ง์„

while len(cur)<len(target)์œผ๋กœ ํ•ด๋ฒ„๋ฆฌ๋ฉด recursionlimit ์„ค์ • ์•ˆํ•ด๋„ ๋˜์–ด์š” ๐Ÿฅน๐Ÿฅน

์ข‹์€ ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌํ•˜๋ฏ€๋‹ˆ๋‹น ๐Ÿ‘

@tgyuuAn
Copy link
Member

tgyuuAn commented May 16, 2024

@H0ngJu ํ™์ฅฌ๋ฅด ์ด๊ฑฐ ํ˜น์‹œ https://discord.com/channels/1203381153764089958/1211331998010834945 ์—ฌ๊ธฐ ๊ทธ๋ฆฌ๋””๋กœ ๋ถ„๋ฅ˜ํ•ด์ฃผ์…”์„œ ์˜ฌ๋ ค์ฃผ์‹ค ์ˆ˜ ์žˆ๋‚˜์š” ?

๊ฐœ์ธ์ ์œผ๋กœ ์ „ ๋จธ๋ฆฌ ํƒ ์น˜๋Š” ๋ฌธ์ œ์˜€๋Š”๋ฐ

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

์ €๋‘ ์žฌ๊ท€๋กœ ์•ˆํ’€๊ณ  ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ’€์—ˆ์–ด์š”!
๋ญ”๊ฐ€ ๋น„๋ฐ€์ด ์ˆจ๊ฒจ์ ธ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ ๋ฐ˜๋Œ€๋กœ ํ‘ธ๋Š”๊ฒŒ ๋‹ต์ด์—ˆ๋„ค์š”. ๊ทธ๋ฆฌ๋””๋„ DP ์ฒ˜๋Ÿผ ์•„์ด๋””์–ด๊ฐ€ ์ค‘์š”ํ•œ ๊ฒƒ ๊ฐ™์•„์š”..!

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val s = br.readLine()!!
    val t = br.readLine()!!

    println(if (sisT(s, t)) 1 else 0)
}

private fun sisT(s: String, t: String): Boolean {
    var now = t
    while (s.length < now.length) {
        now = if (now.last() == 'B') {
            now.substring(0, now.lastIndex).reversed()
        } else {
            now.substring(0, now.lastIndex)
        }
    }
    return s == now
}

@alstjr7437
Copy link
Member

์•„ DFS๋กœ ํ’€๋•Œ ํ•œ๋ฒˆ์”ฉ ์žฌ๊ท€ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œจ๋Š”๋ฐ ์ €๊ฐ€ ์ž˜๋ชป ํ‘ผ๊ฑฐ์ธ์ง€ ํ• ๋•Œ๊ฐ€ ์žˆ๋”๋ผ๊ตฌ์š”...

์ €๋„ ๋‹ค๋ฅธ ๋ถ„๋“ค๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ ์žฌ๊ท€๋กœ๋„ ํ•œ๋ฒˆ ํ’€์–ด๋ด์•ผ๊ฒ ๋„ค์š”!!

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