結果

問題 No.193 筒の数式
ユーザー MisterMister
提出日時 2020-04-12 14:49:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 1,000 ms
コード長 1,061 bytes
コンパイル時間 966 ms
コンパイル使用メモリ 73,384 KB
最終ジャッジ日時 2025-01-09 17:42:14
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using lint = long long;
constexpr lint INF = 1LL << 60;

void solve() {
    std::string s;
    std::cin >> s;

    lint ans = -INF;
    for (int q = 0; q < (int)s.length(); ++q) {
        std::rotate(s.begin(), s.begin() + 1, s.end());
        if (!std::isdigit(s.front()) ||
            !std::isdigit(s.back())) continue;

        lint acc = 0, x = 0;
        char prev = '+';
        for (char c : s) {
            if (std::isdigit(c)) {
                x = x * 10 + (c - '0');
            } else {
                if (prev == '+') {
                    acc += x;
                } else {
                    acc -= x;
                }

                x = 0;
                prev = c;
            }
        }

        if (prev == '+') {
            acc += x;
        } else {
            acc -= x;
        }

        ans = std::max(ans, acc);
    }

    std::cout << ans << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0