ABC解説

日鉄ソリューションズプログラミングコンテスト2023(AtCoder Beginner Contest 303)

A. Similar String

import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    s = str(input().rstrip('\n')).replace("1", "l").replace("0", "o")
    t = str(input().rstrip('\n')).replace("1", "l").replace("0", "o")
    print("Yes" if s == t else "No")


if __name__ == '__main__':
    solve()

B. Discord

import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, input().rstrip('\n').split()))
    a = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]
    ls = [[False] * n for _ in range(n)]
    for i in range(n):
        ls[i][i] = True
    for i in range(m):
        for j in range(1, n):
            ls[a[i][j]-1][a[i][j-1]-1] = True
            ls[a[i][j-1]-1][a[i][j]-1] = True
    cnt = 0
    for i in range(n):
        for j in range(n):
            cnt += 1 if not ls[i][j] else 0
    print(cnt // 2)


if __name__ == '__main__':
    solve()

C. Dash

import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, m, h, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    d = collections.defaultdict(int)
    for i in range(m):
        x, y = list(map(int, input().rstrip('\n').split()))
        d[x, y] = 1
    x, y = 0, 0
    for i in range(n):
        h -= 1
        if h < 0:
            print("No")
            exit()
        if s[i] == "R":
            x += 1
        if s[i] == "L":
            x -= 1
        if s[i] == "U":
            y += 1
        if s[i] == "D":
            y -= 1
        if d[x, y] == 1:
            if h < k:
                d[x, y] -= 1
                h = k
    print("Yes")


if __name__ == '__main__':
    solve()

D. Shift vs. CapsLock

import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    x, y, z = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    dp = [[pow(10, 30)] * (len(s) + 1) for _ in range(2)]
    dp[0][0] = 0
    for i in range(len(s)):
        if s[i] == "a":
            dp[0][i+1] = min(dp[0][i+1], dp[0][i] + x, dp[1][i] + z + x)
            dp[1][i+1] = min(dp[1][i+1], dp[1][i] + y, dp[0][i] + z + y)
        else:
            dp[0][i+1] = min(dp[0][i+1], dp[0][i] + y, dp[1][i] + z + y)
            dp[1][i+1] = min(dp[1][i+1], dp[1][i] + x, dp[0][i] + z + x)
    print(min(dp[0][-1], dp[1][-1]))


if __name__ == '__main__':
    solve()

-ABC解説