목차

Game of Names

ps
링크acmicpc.net/…
출처BOJ
문제 번호35127
문제명Game of Names
레벨플래티넘 3
분류

게임이론

시간복잡도O(∑n)
인풋사이즈∑n <= 2*10^5
사용한 언어Python 3.13
제출기록32412KB / 224ms
최고기록224ms
해결날짜2026/01/17

풀이

처음 풀었던 (좀더 복잡하게 푸는) 풀이

코드

"""Solution code for "BOJ 35127. Game of Names".

- Problem link: https://www.acmicpc.net/problem/35127
- Solution link: http://www.teferi.net/ps/problems/boj/35127

Tags: [game theory]
"""

import sys
from teflib import psutils


@psutils.run_n_times
def main():
    n = int(sys.stdin.readline())
    s = sys.stdin.readline().rstrip()

    if s.count('.') == n:
        print('bob' if n > 1 else 'alice')
        return

    left, right = None, None
    if s[0] != '.':
        left = s[0]
    elif s[1] != '.':
        left = 'a' if s[1] == 'b' else 'b'
    if s[-1] != '.':
        right = s[-1]
    elif s[-2] != '.':
        right = 'a' if s[-2] == 'b' else 'b'

    if left is None and right is None:
        left, right = 'a', 'b'
    elif left is None:
        left = 'a'
    elif right is None:
        right = 'a'

    adv = s.count('b') - s.count('a')
    if left == right:
        adv += 1 if left == 'a' else -1

    print('alice' if adv > 0 else 'bob')


if __name__ == '__main__':
    main()