結果

問題 No.193 筒の数式
ユーザー nu50218
提出日時 2019-09-03 14:47:12
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 1,470 bytes
コンパイル時間 771 ms
コンパイル使用メモリ 72,960 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-24 02:40:02
合計ジャッジ時間 1,507 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <limits>
#include <tuple>
#include <utility>

int number(std::string::const_iterator& state) {
    int res = 0;
    while (isdigit(*state)) {
        res *= 10;
        res += *state - '0';
        state++;
    }
    return res;
}

// value,is_valid
std::tuple<int, bool> eval(std::string s) {
    if (!isdigit(s[0]) || !isdigit(s[s.size() - 1])) {
        return std::make_pair(0, false);
    }
    for (size_t i = 0; i + 1 < s.size(); i++) {
        if (!isdigit(s[0]) && !isdigit(s[1])) {
            return std::make_pair(0, false);
        }
    }

    std::string::const_iterator state = s.begin();
    int res = number(state);
    while (state != s.end()) {
        switch (*state) {
            case '+':
                state++;
                res += number(state);
                break;
            case '-':
                state++;
                res -= number(state);
                break;
        }
    }
    return std::make_pair(res, true);
}

int main() {
    std::string S;
    std::cin >> S;
    int ans = std::numeric_limits<int>::min();
    for (size_t i = 0; i < S.size(); i++) {
        std::string tmp = "";
        for (size_t j = 0; j < S.size(); j++) {
            tmp += S[(i + j) % S.size()];
        }
        int value;
        bool is_valid;
        std::tie(value, is_valid) = eval(tmp);
        if (is_valid) {
            ans = std::max(ans, value);
        }
    }
    std::cout << ans << std::endl;
}
0