ABC解説

AtCoder Beginner Contest 293

A. Swap Odd and Even

import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    s = list(str(input().rstrip('\n')))
    for i in range(len(s) // 2):
        s[i * 2], s[i * 2 + 1] = s[i * 2 + 1], s[i * 2]
    print("".join(s))


if __name__ == '__main__':
    solve()

B. Call the ID Number

import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    ans = [i + 1 for i in range(n)]
    for i in range(n):
        if ans[i] == i + 1:
            ans[a[i]-1] = -1
    ans = list(filter(lambda x: x != -1, ans))
    print(len(ans))
    print(*ans)


if __name__ == '__main__':
    solve()

C. Make Takahashi Happy

import copy
import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    h, w = list(map(int, input().rstrip('\n').split()))
    a = [list(map(int, input().rstrip('\n').split())) for _ in range(h)]
    d = collections.defaultdict(int)
    d[a[0][0]]
    ql = [[0, 0, 0, d]]
    ql = collections.deque(ql)
    ans = 0
    while True:
        if len(ql) != 0:
            cost, xv, yv, d = ql.popleft()
            for xv, yv in [[xv + 1, yv], [xv, yv + 1]]:
                if 0 <= xv < h and 0 <= yv < w:
                    if a[xv][yv] not in d:
                        if xv == h-1 and yv == w-1:
                            ans += 1
                        else:
                            cd = copy.deepcopy(d)
                            cd[a[xv][yv]]
                            ql.append([cost + 1, xv, yv, cd])
        else:
            break
    print(ans)


if __name__ == '__main__':
    solve()

-ABC解説