結果

問題 No.672 最長AB列
ユーザー null_nullnull_null
提出日時 2019-05-24 19:17:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 411 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 176,492 KB
実行使用メモリ 16,180 KB
最終ジャッジ日時 2023-10-17 12:01:50
合計ジャッジ時間 2,992 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 132 ms
16,180 KB
testcase_10 AC 104 ms
9,928 KB
testcase_11 AC 97 ms
9,928 KB
testcase_12 AC 16 ms
4,348 KB
testcase_13 AC 16 ms
4,348 KB
testcase_14 AC 15 ms
4,348 KB
testcase_15 AC 16 ms
4,348 KB
testcase_16 AC 15 ms
4,348 KB
testcase_17 AC 100 ms
9,928 KB
testcase_18 AC 8 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
  string s;
  cin >> s;

  int N = s.size();
  map<int, int> m;
  int p = 0;
  m[0] = -1;

  int ans = 0;
  for (int i = 0; i < N; i++) {
    char c = s[i];
    if (c == 'A')
      p++;
    else
      p--;

    if (m.count(p)) ans = max(ans, i - m[p]);
    if (!m.count(p)) m[p] = i;
  }

  cout << ans << endl;
  return 0;
}
0