ABC285参加記

4完28分 1071位

A問題

$a \times 2 = b$ か $a \times 2 + 1 = b$を満たすか確認する。

import std;

void main() {
    int a, b;
    readf("%d %d\n", a, b);

    writeln(a * 2 == b || a * 2 + 1 == b ? "Yes" : "No");
}

B問題

$i = 1,2,…,N-1$に対して全探索を行う。

import std;

void main() {
    int N;
    string S;
    readf("%d\n%s\n", N, S);

    foreach (i; 1 .. N) {
        int res;
        foreach (j; 0 .. N-i) {
            if (S[j] == S[i+j]) break;
            ++res;
        }

        res.writeln;
    }
}

C問題

文字列を10進数に変換する。

import std;

enum long L = 26;

void main() {
    string S;
    readf("%s\n", S);

    long res, cnt = 1;
    foreach_reverse (s; S) {
        res += cnt * (s - 'A' + 1);
        cnt *= L;
    }

    res.writeln;
}

D問題

BFSを用いて、全てのユーザのユーザ名を希望通り変更することができるか判定する。

import std;

void main() {
    int N;
    readf("%d\n", N);

    bool[string] used;
    int[string] nums;
    auto S = new string[](N), T = new string[](N);
    foreach (i; 0 .. N) {
        readf("%s %s\n", S[i], T[i]);

        used[S[i]] = true;
        nums[S[i]] = i.to!int;
    }

    int[] nouse;
    int[string] wait;
    foreach (i, t; T) {
        if (t !in used) nouse ~= i.to!int;
        else wait[S[nums[t]]] = i.to!int;
    }

    int cnt;
    while (!nouse.empty) {
        auto f = nouse.front;
        nouse.popFront;

        used[T[f]] = true;

        if (S[f] in wait) {
            nouse ~= wait[S[f]];
            wait.remove(S[f]);
        }

        ++cnt;
    }

    writeln(cnt == N ? "Yes" : "No");
}