結果
| 問題 |
No.193 筒の数式
|
| コンテスト | |
| ユーザー |
data9824
|
| 提出日時 | 2015-05-22 03:13:19 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,000 ms |
| コード長 | 1,287 bytes |
| コンパイル時間 | 472 ms |
| コンパイル使用メモリ | 61,272 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-06 04:26:05 |
| 合計ジャッジ時間 | 1,089 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 |
ソースコード
#include <iostream>
#include <string>
#include <limits>
#include <algorithm>
using namespace std;
int evaluate(const string& s) {
enum State {
STATE_OPERATOR,
STATE_NUMBER
};
State state = STATE_OPERATOR;
int result = 0;
char lastOperator = 0;
int number = 0;
for (size_t i = 0; i <= s.size(); ++i) {
char ch = s.c_str()[i];
switch (state) {
case STATE_OPERATOR:
if (isdigit(ch)) {
number *= 10;
number += (int)(ch - '0');
state = STATE_NUMBER;
} else {
return numeric_limits<int>::min();
}
break;
case STATE_NUMBER:
if (isdigit(ch)) {
number *= 10;
number += (int)(ch - '0');
} else if (ch == '+' || ch == '-') {
result += number * (lastOperator == '-' ? -1 : 1);
number = 0;
lastOperator = ch;
state = STATE_OPERATOR;
} else if (ch == 0) {
result += number * (lastOperator == '-' ? -1 : 1);
return result;
} else {
return numeric_limits<int>::min();
}
break;
}
}
return numeric_limits<int>::min();
}
int main() {
string s;
cin >> s;
int result = numeric_limits<int>::min();
for (size_t start = 0; start < s.size(); ++start) {
string expression = s.substr(start) + s.substr(0, start);
result = max(result, evaluate(expression));
}
cout << result << endl;
return 0;
}
data9824