<commit_before>// -------------------------------------------------------------------------------------
//         Author: <NAME> (cbrghostrider); Copyright - All rights reserved.
//                       For email, run on linux (perl v5.8.5):
//   perl -e 'print pack "H*","736f75726162682e732e6a6f73686940676d61696c2e636f6d0a"'
// -------------------------------------------------------------------------------------

// This solution currently gives memory limit exceeded
// Presumably because some degenerate input case is leading to too much recursion
//
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <utility>
#include <assert.h>

using std::pair;
using std::make_pair;
using std::string;
using std::vector;

class Solution {
    // expr = NUMBER | '(' expr ')' | expr '+' expr | expr '-' expr
    
    enum Operator {PLUS, MINUS};
    
    string eatWhiteSpaces(string s) {
        string ret;
        for (int i=0; i<s.size(); i++) {if (s[i] != ' ') ret.push_back(s[i]);}
        return ret;
    }
    
    pair<int, string> evalExprNumber(string str, bool isneg) {
        auto pos = str.find_first_of("+-()");
        string nstr = str.substr(0, pos);
        string rest = pos == string::npos ? "" : str.substr(pos);
        int mult = isneg ? -1 : 1;
        return make_pair(stoi(nstr) * mult, rest);
    }
    
    pair<int, string> evalExprBrackets(string s, bool isneg) {
        assert(s[0] == '(');
        pair<int, string> vsp = expr(s.substr(1), false);
        string retstr = vsp.second.substr(1); //drop trailing ')'
        int mult = isneg ? -1 : 1;
        return make_pair(vsp.first * mult, retstr);
    }
    
    pair<int, string> eval(string s, bool isneg) {
        if (s[0] == '(') {
            return evalExprBrackets(s, isneg);
        } else {
            return evalExprNumber(s, isneg);
        }
    }
    
    //computes this string
    pair<int, string> expr(string s, bool isneg) {
        if (s.length() == 0) return make_pair(0, "");
        pair<int, string> vsp = eval(s, isneg);
        
        string& rest = vsp.second;
        if (rest.empty() || rest[0] == ')') {
            return vsp;
        }
        
        bool passneg = rest[0] == '-';
        rest = rest.substr(1);
        pair<int, string> rvsp = expr(rest, passneg);
        
        return make_pair(vsp.first + rvsp.first, rvsp.second);
    }
    
public:
    int calculate(string s) {
        s = eatWhiteSpaces(s);
        auto compute = expr(s, false);
        return compute.first;
    }
};<commit_msg>Recursive solution.<commit_after>// -------------------------------------------------------------------------------------
//         Author: <NAME> (cbrghostrider); Copyright - All rights reserved.
//                       For email, run on linux (perl v5.8.5):
//   perl -e 'print pack "H*","736f75726162682e732e6a6f73686940676d61696c2e636f6d0a"'
// -------------------------------------------------------------------------------------
// currently getting TLE for a really really big input (taking 2.5 seconds)

#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <utility>
#include <assert.h>
#include "calc.hpp"

using std::pair;
using std::make_pair;
using std::string;
using std::vector;

class Solution {
    // expr = NUMBER | '(' expr ')' | expr '+' expr | expr '-' expr
    
    enum Operator {PLUS, MINUS};

    void eatWhiteSpaces(string& s) {
        int cp=0;
        for (int rp=0; rp<s.size(); rp++) {
            if (s[rp] != ' ') {
                s[cp++] = s[rp];
            }
        }
        s.erase(cp, string::npos);
    }
    
    int evalExprNumber(string& str, bool isneg) {
        auto pos = str.find_first_of("+-()");
        string nstr = str.substr(0, pos);
        str = pos == string::npos ? "" : str.substr(pos);
        int mult = isneg ? -1 : 1;
        return (stoi(nstr) * mult);
    }
    
    int evalExprBrackets(string& s, bool isneg) {
        s = s.substr(1); //drop leading '('
        int val = expr(s, false);
        s = s.substr(1); //drop trailing ')'
        int mult = isneg ? -1 : 1;
        return (val * mult);
    }
    
    int eval(string& s, bool isneg) {
        if (s[0] == '(') {
            return evalExprBrackets(s, isneg);
        } else {
            return evalExprNumber(s, isneg);
        }
    }
    
    //computes this string
    int expr(string& s, bool isneg) {
        if (s.length() == 0) return 0;
        int val = eval(s, isneg);
        
        if (s.empty() || s[0] == ')') {
            return val;
        }
        
        bool passneg = s[0] == '-';
        s = s.substr(1);
        int rval = expr(s, passneg);
        
        return (val + rval);
    }
    
public:
    int calculate(string s) {
        s = eatWhiteSpaces(s);
        return expr(s, false);
    }
};

int main()
{
    string s(EXPRCALC);
    Solution sln;
    int ans = sln.calculate(s);
    std::cout << ans << std::endl;
}
{'fraction_non_alphanumeric': 0.2121760096443641, 'fraction_numerical': 0.024110910186859555, 'mean_word_length': 1.6394485683987274, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4140449', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1594, 'n_words': 410}