結果

問題 No.2018 X-Y-X
コンテスト
ユーザー vjudge1
提出日時 2026-03-22 23:19:11
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 848 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,848 ms
コンパイル使用メモリ 181,584 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2026-03-22 23:19:20
合計ジャッジ時間 8,874 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
long long ans;
int n, a[1000005], b[1000005], A[1000005], B[1000005];
string s, t;
int main()
{
	cin >> n;
	cin >> s >> t;
	if (s[0] != t[0] || s[n - 1] != t[n - 1])
	{
		cout << -1;
		return 0;
	}
	for (int i = 1; i < n; i++)
	{
        if (i & 1)
        {
    		a[i] = s[i - 1] != s[i];
    		b[i] = t[i - 1] != t[i];
        }
        else
        {
    		a[i] = s[i - 1] == s[i];
    		b[i] = t[i - 1] == t[i];
        }
        ans += a[i] - b[i];
	}
    if (ans)
    {
        cout << -1;
        return 0;
    }
    for (int i = 1; i <= n; i++)
    {
        if (a[i])
        {
            A[++A[0]] = i;
        }
        if (b[i])
        {
            B[++B[0]] = i;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        ans += abs(A[i] - B[i]);
    }
    cout << ans;
	return 0;
}
0