結果

問題 No.672 最長AB列
ユーザー misora192misora192
提出日時 2020-05-21 08:11:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,145 bytes
コンパイル時間 1,707 ms
コンパイル使用メモリ 167,800 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 23:15:06
合計ジャッジ時間 2,621 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 7 ms
6,944 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 WA -
testcase_12 AC 5 ms
6,944 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 7 ms
6,940 KB
testcase_18 AC 4 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

int n;
vector<int> a, b;

bool cond(int x){
	if(2*x > n) return false;
	for(int i = 0; i + 2*x <= n; i++){
		if((a[i+2*x] - a[i]) == (b[i+2*x] - b[i])) return true;
	}

	return false;
}

// condを満たす最大のxを求める(upper_bound)
// 全てのxが条件を満たさない場合は-1を返す(-1はとりえない値)
int upper_bound_cond(){
	int lb = -1, ub = n;

	while(ub - lb > 1){
		int mid = (lb + ub) / 2;

		if(cond(mid)) lb = mid;   // Answer is in [mid, ub)
		else ub = mid;            // Answer is in [lb, mid)
	}

	// now lb + 1 = ub
	return lb;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	string s;
	cin >> s;

	n = s.size();
	a.resize(n+1, 0);
	b.resize(n+1, 0);

	rep(i, n){
		a[i+1] = a[i] + (s[i] == 'A' ? 1 : 0);
		b[i+1] = b[i] + (s[i] == 'B' ? 1 : 0);
	}

	cout << max(2 * upper_bound_cond(), 0) << endl;
}
0