ABC003 B - AtCoderトランプ

備忘録

問題

atcoder.jp

回答

import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    T = iss()
    S = iss()
    at = {'a', 't', 'c', 'o', 'd', 'e', 'r'}

    for n in range(len(T)):
        if T[n] == S[n] == '@': continue
        if T[n] != '@' and S[n] != '@' and T[n] != S[n]:
            print('You will lose')
            exit()
        if T[n] != '@' and T[n] not in at and T[n] != S[n] and S[n] not in at:
            print('You will lose')
            exit()
        if S[n] != '@' and S[n] not in at and S[n] != T[n] and T[n] not in at:
            print('You will lose')
            exit()
        if (T[n] == '@' and S[n] not in at) or (S[n] == '@' and T[n] not in at):
            print('You will lose')
            exit()
    else:
        print('You can win')


if __name__ == '__main__':
    main()

考え方

愚直に起こりえる負けパターンを列挙し、
全てに一致しない場合には勝ちとした。

非常にわかりにくいため、他の方の回答を元に
分かりやすい実装にしたのか下記。

def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    T = iss()
    S = iss()
    at = {'a', 't', 'c', 'o', 'd', 'e', 'r', '@'}

    for t, s in zip(T, S):
        if t == s:
            continue
        elif t == '@' or s == '@':
            if t in at and s in at:
                continue
        print('You will lose')
        exit()
    else:
        print('You can win')

文字列Tの文字と文字列Sの文字が一致している場合と、
文字列Tの文字と文字列Sの文字が@の場合には、
どちらも置き換えることが出来る文字に入っているか否かを確かめる。
圧倒的にわかりやすい。。