結果

問題 No.672 最長AB列
ユーザー ats5515ats5515
提出日時 2018-04-13 22:26:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 85,956 KB
実行使用メモリ 29,768 KB
最終ジャッジ日時 2023-09-10 00:03:05
合計ジャッジ時間 2,662 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 199 ms
29,768 KB
testcase_10 AC 125 ms
17,440 KB
testcase_11 AC 119 ms
17,360 KB
testcase_12 AC 16 ms
4,952 KB
testcase_13 AC 16 ms
4,952 KB
testcase_14 AC 16 ms
5,008 KB
testcase_15 AC 17 ms
4,956 KB
testcase_16 AC 17 ms
5,068 KB
testcase_17 AC 116 ms
17,368 KB
testcase_18 AC 6 ms
5,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string S;
	cin >> S;
	int N = S.size();
	vector<int> A(N + 1, 0);
	int res = 0;
	for (int i = 0; i < N; i++) {
		if (S[i] == 'A') {
			A[i + 1] = A[i] + 1;
		}
		else {
			A[i + 1] = A[i] - 1;
		}
	}
	map<int, int> mpr;
	map<int, int> mpl;
	for (int i = 0; i < N + 1; i++) {
		mpr[A[i]] = i;
	}
	for (int i = N ; i >= 0; i--) {
		mpl[A[i]] = i;
	}
	for (auto x : mpl) {
		if (mpr.count(x.first)) {
			res = max(res, mpr[x.first] - x.second);
		}
	}
	cout << res << endl;
}
0