ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 13022 ๋Š‘๋Œ€์™€ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด (String)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 8. 23:34

    ๐Ÿ“Œ ๋ฌธ์ œ

    ๋‹ค์Œ์€ ๋Š‘๋Œ€ ๋‚˜๋ผ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค.

     

    ์ž„์˜์˜ ์–‘์˜ ์ •์ˆ˜ n์— ๋Œ€ํ•ด์„œ, 'w'๊ฐ€ n๋ฒˆ ๋‚˜์˜ค๊ณ , ๊ทธ ๋‹ค์Œ์— 'o'๊ฐ€ n๋ฒˆ, ๊ทธ ๋‹ค์Œ์— 'l'์ด n๋ฒˆ, ๊ทธ ๋‹ค์Œ์— 'f'๊ฐ€ n๋ฒˆ ๋‚˜์˜จ ๋‹จ์–ด๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์ด๋‹ค.
    ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด ๋‘ ๊ฐœ๋ฅผ ์ด์€ ๋‹จ์–ด๋„ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์ด๋‹ค.
    1๋ฒˆ๊ณผ 2๋ฒˆ ์กฐ๊ฑด์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋งŒ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์ด๋‹ค.


    ๋‹ค์Œ์€ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์˜ ์˜ˆ์‹œ์ด๋‹ค.

    1๋ฒˆ ๊ทœ์น™์œผ๋กœ ๋งŒ๋“  "wolf", "wwoollff", "wwwooolllfff"๋Š” ๋ชจ๋‘ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์ด๋‹ค.
    2๋ฒˆ ๊ทœ์น™์œผ๋กœ ๋งŒ๋“  "wolfwwoollff"์€ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์ด๋‹ค.
    2๋ฒˆ ๊ทœ์น™์„ ๋‘ ๋ฒˆ ์จ์„œ ๋งŒ๋“  "wolfwwoollffwolf"์€ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์ด๋‹ค.
    "wfol"์€ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋‹ค. (์ˆœ์„œ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Œ)
    "wwolfolf"๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋‹ค. (๋ฌธ์ž์—ด์˜ ์ค‘๊ฐ„์— ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ์ง‘์–ด ๋„ฃ์Œ)
    "wwwoolllfff"๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋‹ค. (o๊ฐ€ 2๋ฒˆ ๋“ค์–ด๊ฐ”๋‹ค)

    ์ž…๋ ฅ

    ์ฒซ์งธ ์ค„์— ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด๋Š” w, o, l, f๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 50์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

    ์ถœ๋ ฅ

    ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๋‹จ์–ด์ธ ๊ฒฝ์šฐ์—๋Š” 1์„, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    wolf

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    1

    ์˜ˆ์ œ ์ž…๋ ฅ 2

    wwolfolf

    ์˜ˆ์ œ ์ถœ๋ ฅ 2

    0

     


    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    input = sys.stdin.readline
    
    
    def check(seq, dic):
        if ''.join(seq) == 'wolf' and dic['w'] == dic['o'] == dic['l'] == dic['f']:
            seq = []
            dic = dict(w=0, o=0, l=0, f=0)
        else:
            print(0)
            sys.exit(0)
        return seq, dic
    
    
    word = list(input().rstrip())
    if len(word) < 4:
        print(0)
        sys.exit(0)
    
    # wolf ์ˆœ์„œ ์ฒดํฌ๋ฅผ ์œ„ํ•œ seq
    former = word[0]
    seq = [former]
    
    # wolf ๊ฐœ์ˆ˜ ์ฒดํฌ๋ฅผ ์œ„ํ•œ dic
    dic = dict(w=0, o=0, l=0, f=0)
    dic[former] = 1
    
    for i in range(1, len(word)):
        dic[word[i]] += 1
        
        if word[i] != former:
            if former == 'f':
                seq, dic = check(seq, dic)
            seq.append(word[i])
            former = word[i]
    
        if i == len(word) - 1:
            seq, dic = check(seq, dic)
    
    print(1)

    ๐Ÿ’ก Solution

    ๋‹จ์–ด๊ฐ€ wolf ์ˆœ์„œ๋Œ€๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ → seq ๋ฆฌ์ŠคํŠธ๋กœ ์ฒดํฌ
    ๋‹จ์–ด์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ ๊ฐ™์€ ํšŸ์ˆ˜(n๋ฒˆ)๋กœ ๋“ฑ์žฅํ•˜๋Š”์ง€ → dic ๋ฆฌ์ŠคํŠธ๋กœ ์ฒดํฌ

    ์ด ๋‘ ๊ฐ€์ง€๋ฅผ ์ฒดํฌํ•ด์„œ ๋‘˜ ๋‹ค ๋งŒ์กฑํ•˜๋ฉด 1์„ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ๋œฏ์–ด๋ณด๋ฉด

    word = list(input().rstrip())
    if len(word) < 4:
        print(0)
        sys.exit(0)

    ๋‹จ์–ด๊ฐ€ 4๊ธ€์ž ์ดํ•˜๋ฉด wolf๋ฅผ ์™„์„ฑ์‹œํ‚ฌ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— 0์„ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์•ˆํ•ด์คฌ๋”๋‹ˆ ์ž๊พธ 99%๊นŒ์ง€ ๊ฐ”๋‹ค๊ฐ€ ์˜ค๋‹ตํŒ์ •์„ ๋ฐ›์•„์„œ ํ—ค๋งธ์Šต๋‹ˆ๋‹ค ใ… ใ… 

    # wolf ์ˆœ์„œ ์ฒดํฌ๋ฅผ ์œ„ํ•œ seq
    former = word[0]
    seq = [former]
    
    # wolf ๊ฐœ์ˆ˜ ์ฒดํฌ๋ฅผ ์œ„ํ•œ dic
    dic = dict(w=0, o=0, l=0, f=0)
    dic[former] = 1
    • ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๋งˆ๋‹ค seq์— ์ถ”๊ฐ€
      ๊ทธ๋Ÿฌ๋ ค๋ฉด ์ด์ „ ๋ฌธ์ž๊ฐ€ ๋ญ”์ง€ ์•Œ์•„์•ผ ํ•˜๋ฏ€๋กœ former์— ์ด์ „ ๋ฌธ์ž๋ฅผ ์ €์žฅ
    • w, o, l, f์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•œ ๋”•์…”๋„ˆ๋ฆฌ dic ์ƒ์„ฑ

    for i in range(1, len(word)):
        dic[word[i]] += 1
        
        if word[i] != former:
            if former == 'f':
                seq, dic = check(seq, dic)
            seq.append(word[i])
            former = word[i]
    
        if i == len(word) - 1:
            seq, dic = check(seq, dic)
    • ํ•ด๋‹น ๋ฌธ์ž ๊ฐœ์ˆ˜ +1
    • ์ด์ „ ๋ฌธ์ž์™€ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด seq์— ์ถ”๊ฐ€ํ•˜๊ณ , former ์—…๋ฐ์ดํŠธ
    • ์ด์ „ ๋ฌธ์ž์™€ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ๋‚˜์™”๋Š”๋ฐ ์ด์ „ ๋ฌธ์ž๊ฐ€ f์˜€๋‹ค๋ฉด wolf ํ•œ์„ธํŠธ๊ฐ€ ๋๋‚ฌ์„ ๊ฒƒ์ด๋ฏ€๋กœ check(wolf์˜ ์ˆœ์„œ์™€ ๊ฐœ์ˆ˜๋ฅผ ์ฒดํฌํ•˜๋Š” ํ•จ์ˆ˜) ์‹คํ–‰
    • ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ผ๋ฉด check ์‹คํ–‰

    def check(seq, dic):
        if ''.join(seq) == 'wolf' and dic['w'] == dic['o'] == dic['l'] == dic['f']:
            seq = []
            dic = dict(w=0, o=0, l=0, f=0)
        else:
            print(0)
            sys.exit(0)
        return seq, dic
    • seq๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์„œ wolf์™€ ๋™์ผํ•œ์ง€, w, o, l, f์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ชจ๋‘ ๋™์ผํ•œ์ง€ ์ด ๋‘ ๊ฐœ๋ฅผ ๋ชจ๋‘ ๋งŒ์กฑํ•œ๋‹ค๋ฉด seq์™€ dic์„ ๋น„์›Œ์ฃผ๊ณ  ๋‹ค์Œ ๋ฌธ์ž ํƒ์ƒ‰ํ•˜๋Ÿฌ~
    • ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•œ๋‹ค๋ฉด 0์„ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ!

     

     

     

     

    ๋Œ“๊ธ€

Designed by Tistory.