結果

問題 No.672 最長AB列
ユーザー srup٩(๑`н´๑)۶
提出日時 2018-06-11 11:20:40
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 665 bytes
コンパイル時間 1,400 ms
コンパイル使用メモリ 168,588 KB
実行使用メモリ 16,768 KB
最終ジャッジ日時 2024-06-30 13:29:44
合計ジャッジ時間 2,704 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)

int main(int argc, char const *argv[])
{
	string s; cin >> s;
	vector<int> cnt(s.size() + 1);

	int now = 0;
	cnt[0] = 0;
	rep(i, s.size()) {
		if (s[i] == 'A') now++;
		else now--;

		cnt[i + 1] = now;
	}

	map<int, pair<int, int>> ma;
	rep(i, s.size() + 1) {
		if(ma.count(cnt[i])) {
			ma[cnt[i]] = make_pair(ma[cnt[i]].first, i);
		} else {
			ma[cnt[i]] = make_pair(i, i);
		}
	}

	int ans = 0;
	for (auto u : ma) {
		ans = max(ans, u.second.second - u.second.first);
	}
	printf("%d\n", ans);
	return 0;
}
0