結果

問題 No.222 引き算と足し算
ユーザー not_522not_522
提出日時 2015-08-04 07:48:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,802 bytes
コンパイル時間 1,874 ms
コンパイル使用メモリ 190,736 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-27 17:46:22
合計ジャッジ時間 2,524 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template<typename T> inline T toInteger(const string&);

template<> inline int toInteger<int>(const string& s) {
  return stoi(s);
}

template<> inline long toInteger<long>(const string& s) {
  return stol(s);
}

template<> inline long long toInteger<long long>(const string& s) {
  return stoll(s);
}

template<typename T = long long> inline T toInteger(const string& s, int n) {
  T res = 0;
  for (char c : s) {
    if (isdigit(c)) res = res * n + c - '0';
    else if (isalpha(c)) res = res * n + tolower(c) - 'a' + 10;
  }
  return s[0] == '-' ? -res : res;
}

template<typename T = long long> class Parser {
private:
  string s;
  int p;
  unordered_map<char, function<T(T)>> unary_operator;
  vector<unordered_map<char, function<T(T, T)>>> binary_operator;
  unordered_map<char, char> brackets;

  T term() {
    if (brackets.count(s[p])) {
      const char end = brackets[s[p]];
      ++p;
      T res = expr(0);
      if (s[p] != end) throw "mismatch brackets";
      ++p;
      return res;
    }
    if (unary_operator.count(s[p])) {
      auto f = unary_operator[s[p]];
      ++p;
      return f(term());
    }
    // TODO check term is valid or not
    string res = "";
    for (; isdigit(s[p]); ++p) res += s[p];
    return toInteger<T>(res);
  }

  T expr(size_t level) {
    if (level == binary_operator.size()) return term();
    T res = expr(level + 1);
    for (char c; c = s[p++], binary_operator[level].count(c);) res = binary_operator[level][c](res, expr(level + 1));
    --p;
    return res;
  }

public:
  void addUnaryOperator(function<T(T)> func, char c) {
    unary_operator[c] = func;
  }

  void addBinaryOperator(function<T(T, T)> func, char c, size_t level) {
    if (binary_operator.size() <= level) binary_operator.resize(level + 1);
    binary_operator[level][c] = func;
  }

  void addBrackets(char begin, char end) {
    brackets[begin] = end;
  }

  long long parse(const string& s) {
    this->s = s + '@';
    p = 0;
    return expr(0);
  }
};

template<typename T = long long> inline T parse(const string& s) {
  Parser<T> parser;
  parser.addUnaryOperator([](T t){return +t;}, '+');
  parser.addUnaryOperator(negate<T>(), '-');
  parser.addBinaryOperator(plus<T>(), '+', 0);
  parser.addBinaryOperator(minus<T>(), '-', 0);
  parser.addBinaryOperator(multiplies<T>(), '*', 1);
  parser.addBinaryOperator(divides<T>(), '/', 1);
  parser.addBrackets('(', ')');
  return parser.parse(s);
}

int main() {
  Parser<long long> parser;
  parser.addUnaryOperator([](long long t){return +t;}, '+');
  parser.addUnaryOperator(negate<long long>(), '-');
  parser.addBinaryOperator(minus<long long>(), '+', 0);
  parser.addBinaryOperator(plus<long long>(), '-', 0);
  string s;
  cin >> s;
  cout << parser.parse(s) << endl;
}
0