結果

問題 No.672 最長AB列
コンテスト
ユーザー mayoko_
提出日時 2018-04-14 00:23:56
言語 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  
実行時間 6 ms / 2,000 ms
コード長 535 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 378 ms
コンパイル使用メモリ 77,556 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-03-15 21:58:51
合計ジャッジ時間 1,131 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;
const int MAX = 222222;
int initialPoint[MAX*2];
int* p = initialPoint + MAX;

int main() {
	string str;
	cin >> str;
	int cur = 0;
	memset(initialPoint, -1, sizeof(initialPoint));
	p[0] = 0;
	int ans = 0;
	for (int i = 0; i < str.size(); ++i) {
		const char c = str[i];
		if (c == 'A') {
			++cur;
		} else {
			--cur;
		}
		if (p[cur] < 0) {
			p[cur] = i+1;
		} else {
			ans = max(ans, i+1 - p[cur]);
		}
	}
	cout << ans << endl;
}
0