結果

問題 No.672 最長AB列
ユーザー incuiioincuiio
提出日時 2023-05-10 15:40:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 1,268 ms
コンパイル使用メモリ 138,560 KB
実行使用メモリ 16,136 KB
最終ジャッジ日時 2023-08-17 20:41:18
合計ジャッジ時間 3,553 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 140 ms
16,136 KB
testcase_10 AC 95 ms
9,604 KB
testcase_11 AC 89 ms
9,568 KB
testcase_12 AC 14 ms
4,380 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 14 ms
4,380 KB
testcase_15 AC 14 ms
4,380 KB
testcase_16 AC 14 ms
4,376 KB
testcase_17 AC 89 ms
9,652 KB
testcase_18 AC 7 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <list>
#include <climits>
#include <bitset>
#include <numeric>
#include <cassert>
#include <regex>
using std::cout;
using std::cin;
using std::string;
using std::vector;

struct pos
{
    int first, last;
    pos()
    {
        first = last = -1;
    }
};

int main()
{
    string s;
    cin >> s;
    std::map<int, pos> arr;
    arr[0].first = 0;
    arr[0].last = 0;
    int sum = 0;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == 'A')
        {
            sum++;
        }
        else
        {
            sum--;
        }
        if (arr[sum].first == -1)
        {
            arr[sum].first = i+1;
        }
        arr[sum].last = i+1;
    }
    int max = 0;
    for (const auto [k, p] : arr)
    {
        if (p.first == -1)
        {
            continue;
        }
        max = std::max(max, p.last - p.first);
    }
    cout << max;
}
0