結果

問題 No.193 筒の数式
ユーザー 👑 nu50218nu50218
提出日時 2019-09-03 14:47:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,470 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 74,216 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-25 17:16:37
合計ジャッジ時間 1,818 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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