結果

問題 No.2018 X-Y-X
コンテスト
ユーザー December456
提出日時 2025-11-17 22:46:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 762 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 29,044 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-11-17 22:46:33
合計ジャッジ時間 2,193 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘long long int solve()’:
main.cpp:9:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    9 |     scanf("%d%s%s", &n, s, t);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <cstdio>

constexpr int MAX_N = 200000 + 2;

char s[MAX_N], t[MAX_N];

long long solve() {
    int n;
    scanf("%d%s%s", &n, s, t);

    if (s[0] != t[0]) {
        return -1;
    }

    int j = 1;
    long long ans = 0;

    for (int i = 1; i < n; i++) {
        if (i == j) {
            j += s[i] == t[i];
            continue;
        }

        if (s[i] == s[i - 2]) {
            ans += i - (j++);

            s[i - 1] = 'A' + 'B' - s[i - 1];
        }

        if (j < i && t[i] == t[i - 2]) {
            ans += i - (j++);

            t[i - 1] = 'A' + 'B' - t[i - 1];
        }

        if (i == j) {
            j += s[i] == t[i];
        }
    }

    return j < n ? -1 : ans;
}

int main() {
    printf("%lld\n", solve());

    return 0;
}
0