vector<int> diffWaysToCompute(string input) {
		vector<int> res;
		if(input.length()==0)
			return res;
		for (int i = 0; i < input.length(); ++i)
		{
			auto cur = input[i];
			if(cur == '-'||cur == '+'||cur == '*'){
				auto res1 = diffWaysToCompute(input.substr(0,i));
				auto res2 = diffWaysToCompute(input.substr(i+1,input.length()-i-1));
				for(auto r1 : res1){
					for(auto r2 : res2){
						switch(cur){
							case '-':
							res.push_back(r1 - r2);
							break;
							case '+':
							res.push_back(r1 + r2);
							break;
							default:
							res.push_back(r1 * r2);
						}

					}
				}
			}
		}
		if(res.empty())
			res.push_back(atoi(input.c_str()));
		return res;
	}
 vector<int> diffWaysToCompute(string input) {
     vector<int> res;
     int size = input.size();
     for(int i = 0; i < size; ++i) {
         char cur = input[i];
         if(cur == '+' || cur == '-' || cur == '*') {
             vector<int> result1 = diffWaysToCompute(input.substr(0, i));
             vector<int> result2 = diffWaysToCompute(input.substr(i + 1));
             for(auto n1 : result1) {
                 for(auto n2 : result2) {
                     if(cur == '+') 
                         res.push_back(n1 + n2);
                     else if(cur == '-') 
                         res.push_back(n1 - n2);
                     else{
                         res.push_back(n1 * n2);
                     }
                 }
             }
         }
     }
     if(res.empty()) { //输入字符串只包含数字的情况
         res.push_back(atoi(input.c_str()));
     }
     return res;
 }
 vector<int> diffWaysToCompute(string input) {
     vector<int> res;
     for (int i = 0; i < input.length(); ++i)
     {
     	if(input.at(i) == '+' || input.at(i) == '-' || input.at(i) == '*' || input.at(i) == '/')
     	{
     		std::vector<int> left = diffWaysToCompute(input.substr(0, i));
     		std::vector<int> right = diffWaysToCompute(input.substr(i+1));
     		for (int j = 0; j < left.size(); ++j)
     			for(int k=0; k<right.size(); ++k)
     		{
     			switch(input.at(i)){
     				case '+':
     					res.push_back(left[j]+right[k]);
     					break;
     				case '-':
     					res.push_back(left[j]-right[k]);
     					break;
     				case '*':
     					res.push_back(left[j]*right[k]);
     					break;
     				case '/':
     					res.push_back(left[j]/right[k]);
     					break;
     			}
     		}
     	}
     }
     if(res.empty()) res.push_back(atoi(input.c_str()));
     return res;
 }
 vector<int> diffWaysToCompute(string input) {
     vector<int> ret;
     for(int i = 0; i < input.size(); i ++)
     {
         if(input[i] == '+' || input[i] == '-' || input[i] == '*')
         {
             vector<int> left = diffWaysToCompute(input.substr(0, i));
             vector<int> right = diffWaysToCompute(input.substr(i+1));
             for(int j = 0; j < left.size(); j ++)
             {
                 for(int k = 0; k < right.size(); k ++)
                 {
                     if(input[i] == '+')
                         ret.push_back(left[j] + right[k]);
                     else if(input[i] == '-')
                         ret.push_back(left[j] - right[k]);
                     else
                         ret.push_back(left[j] * right[k]);
                 }
             }
         }
     }
     if(ret.empty())
         ret.push_back(atoi(input.c_str()));
     return ret;
 }
 vector<int> diffWaysToCompute(const string& input) {
     vector<int> results;
     for (int i = 0; i < input.length(); ++i) {
         char c = input[i];
         if (c == '-' || c == '*' || c == '+') {
             vector<int> left  = diffWaysToCompute(input.substr(0, i));
             vector<int> right = diffWaysToCompute(input.substr(i + 1));
             for (int x : left) {
                 for (int y : right) {
                     switch(c) {
                         case '-':
                             results.emplace_back(x - y);
                             break;
                         case '*':
                             results.emplace_back(x * y);
                             break;
                         case '+':
                             results.emplace_back(x + y);
                             break;
                     }
                 }
             }
         }
     }
     if (results.empty()) {
         results.emplace_back(std::stoi(input));
     }
     return results;
 }
 vector<int> diffWaysToCompute(const string& input) {
     std::vector<int> result;
     for (size_t i = 0; i < input.size(); ++i) {
         const char c = input[i];
         if (c == '+' || c == '-' || c == '*') {
             std::string leftStr = input.substr(0, i);
             std::string rightStr = input.substr(i + 1);
             std::vector<int> left = diffWaysToCompute(leftStr);
             std::vector<int> right = diffWaysToCompute(rightStr);
             for (size_t j = 0; j < left.size(); ++j) {
                 for (size_t k = 0; k < right.size(); ++k) {
                     switch (c) {
                         case '+':
                             result.push_back(left[j] + right[k]);
                             break;
                         case '-':
                             result.push_back(left[j] - right[k]);
                             break;
                         case '*':
                             result.push_back(left[j] * right[k]);
                             break;
                         default:
                             break;
                     }
                 }
             }
         }
     }
     if (result.empty()) {
         result.push_back(std::stoi(input));
     }
     return result;
 }
 vector<int> diffWaysToCompute(const string& input, unordered_map<string, vector<int> >& cache) {
     vector<int> result;
     for (int i = 0; i < input.size(); ++i) {
         if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
             vector<int> result1, result2;
             string former = input.substr(0, i), latter = input.substr(i + 1);
             auto pos1 = cache.find(former);
             auto pos2 = cache.find(latter);
             if (pos1 == cache.end()) {
                 result1 = diffWaysToCompute(former);
                 cache[former] = result1;
             } else result1 = cache[former];
             if (pos2 == cache.end()) {
                 result2 = diffWaysToCompute(latter);
                 cache[latter] = result2;
             } else result2 = cache[latter];
         
             for (auto r1 : result1)
                 for (auto r2 : result2)
                     if (input[i] == '+') result.push_back(r1 + r2);
                     else if (input[i] == '-') result.push_back(r1 - r2);
                     else result.push_back(r1 * r2);
         }
     }
     if (result.empty()) result.push_back(stoi(input));
     return result;
 }
 vector<int> diffWaysToCompute(string input) {
     vector<int> res;
     int size = input.size();
     for (int i = 0; i < size; i++) {
         if ('+' == input[i] || '-' == input[i] || '*' == input[i]) {
             vector<int> left = diffWaysToCompute(input.substr(0, i));
             vector<int> right = diffWaysToCompute(input.substr(i + 1));
             int leftSize = left.size();
             int rightSize = right.size();
             for (int j = 0; j < leftSize; j++) {
                 for (int k = 0; k < rightSize; k++) {
                     switch(input[i]) {
                         case '+':
                             res.push_back(left[j] + right[k]);
                             break;
                         case '-':
                             res.push_back(left[j] - right[k]);
                             break;
                         case '*':
                             res.push_back(left[j] * right[k]);
                             break;
                         default:
                             break;
                     }
                 }
             }
         }
     }
     if (res.empty()) {
         res.push_back(atoi(input.c_str()));
     }
     return res;
 }
 vector<int> diffWaysToCompute(string input) {
     vector<int> result;
     int index=0;
     int temp=0;
     for (;index<input.size();index++) {
         if (isdigit(input[index])) temp=temp*10+input[index]-'0';
         else break;
     }
     if (index==input.size()) {result.push_back(temp); return result;}
     for (int i=0;i<input.size();i++) {
         if (isdigit(input[i])) continue;
         else {
             vector<int> left=diffWaysToCompute(input.substr(0,i));
             vector<int> right=diffWaysToCompute(input.substr(i+1));
             for (int j=0;j<left.size();j++) {
                 for (int  k=0;k<right.size();k++) {
         
                     switch(input[i]) {
                         case '+': 
                         result.push_back(left[j]+right[k]);
                         break;
                         case '-':
                         result.push_back(left[j]-right[k]);
                         break;
                         case '*':
                         result.push_back(left[j]*right[k]);
                         break;
                             }
                 }
             }
             }
         }
     
     return result;
 }
vector<int> diffWaysToCompute(string input) {
        
        if (express.find(input) != express.end() ) {
            return express[input];
        }
        
        vector<int> res;
        
        for (int i = 0; i < input.size(); i++) {
            if (input[i] == '*' || input[i] == '+' || input[i] == '-') {
                vector<int> left = diffWaysToCompute(input.substr(0, i) );
                vector<int> right = diffWaysToCompute(input.substr(i + 1) );
                for (int l = 0; l < left.size(); l++) {
                    for (int r = 0; r < right.size(); r++) {
                        res.push_back( calc(left[l], right[r], input[i]) );
                    }
                }
            }
        }
        
        if (res.empty() ) {
            res.push_back(stoi(input.c_str() ) );
        }
        express[input] = res;
        
        return res;
    }
 vector<int> diffWaysToCompute(string input) {
         vector<int> output;
 for (int i=0; i<input.size(); i++) {
     char c = input[i];
     if (ispunct(c))
         for (int a : diffWaysToCompute(input.substr(0, i)))
             for (int b : diffWaysToCompute(input.substr(i+1)))
                 output.push_back(c=='+' ? a+b : c=='-' ? a-b : a*b);
 }
 return output.size() ? output : vector<int>{stoi(input)};
 }
    vector<int> diffWaysToCompute(string input) {

        //std::cout<<input<<std::endl;

        int len = input.length();

        vector<int> result;



        int i=findFirstOp(input);

        if(-1==i){

            result.push_back(atoi(input.c_str()));

            return result;

        }

        while(i<len){

            if(input[i]!='*' && input[i]!='+' && input[i]!='-'){

                i++;

                continue;

            }

            vector<int> tmp1 = diffWaysToCompute(input.substr(0,i));

            vector<int> tmp2 = diffWaysToCompute(input.substr(i+1,len-i-1));

            

            for(int b1=0;b1<tmp1.size();b1++){

                for(int b2=0;b2<tmp2.size();b2++){

                    result.push_back(calcone(tmp1[b1],input[i],tmp2[b2]));

                }

            }

            

            i+=2;

        }

    }
    vector<int> diffWaysToCompute(string input) {
        vector<int> result;

        for (int i = 0; i < input.size(); ++i) {
            char c= input[i];
            if (c == '+' || c == '-' || c == '*') {
                for (int a : diffWaysToCompute(input.substr(0, i))) {
                    for (int b : diffWaysToCompute(input.substr(i + 1))) {
                        result.push_back(c == '+' ? a+b : c == '-' ? a-b : a * b);
                    }
                }
            }
        }
        if (result.size() == 0) return vector<int>{stoi(input)};
        else return result;
    }
Exemplo n.º 14
0
 vector<int> diffWaysToCompute(string input) {
     vector<int> res;
     vector<int> indexes;
     for (int i(0); i < input.length(); i++)
         if (!(input[i] >= '0' && input[i] <= '9')) indexes.push_back(i);
     if (indexes.size() == 0) res.push_back(atoi(input.c_str()));
     for (int i(0); i < indexes.size(); i++) {
         string left = input.substr(0, indexes[i]);
         string right = input.substr(indexes[i]+1, input.length()-indexes[i]-1);
         vector<int> res_left = diffWaysToCompute(left);
         vector<int> res_right = diffWaysToCompute(right);
         for (int j(0); j < res_left.size(); j++) {
             for (int k(0); k < res_right.size(); k++)
                 res.push_back(calc(res_left[j], res_right[k], input[indexes[i]]));
         }
     }
     return res;
 }
Exemplo n.º 15
0
 vector<int> diffWaysToCompute(string input) {
     vector<int> result;
     int i = 0;
     for ( ;i < input.size(); ++i) {
         if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
             vector<int> left = diffWaysToCompute(input.substr(0,i));
             vector<int> right = diffWaysToCompute(input.substr(i+1));
             for (auto l : left)
                 for (auto r: right) {
                     result.push_back(compute(l,r,input[i]));
                 }
         }
     }
     
     // indicates pure number, return itself
     if (result.size() == 0) result.push_back(stoi(input));
     return result;
 }
Exemplo n.º 16
0
int main() {
  int size;
  int* result = diffWaysToCompute("2*3-4*5", &size);
  for (int i = 0; i < size; ++i) {
    printf("%d ", result[i]);
  }
  printf("\n");
  free(result);
  return 0;
}
 vector<int> diffWaysToCompute(string input) {
     if(input.empty()) return {};
     const int n = input.size();
     vector<int> res;
     for(int i = 0; i < n; i++){
         int sign = input[i];
         if(sign == '+' || sign == '-' || sign == '*'){
             vector<int> temp1 = diffWaysToCompute(input.substr(0, i));
             vector<int> temp2 = diffWaysToCompute(input.substr(i+1));
             for(int num1 : temp1)
                 for(int num2 : temp2)
                     if(sign == '+') res.push_back(num1 + num2);
                     else if(sign == '-') res.push_back(num1 - num2);
                     else res.push_back(num1 * num2);
         }
     }
     if(res.empty()) return {stoi(input)};
     else return res;
 }
 vector<int> diffWaysToCompute(string input) {
     vector<int> ret;
     for(int i = 0; i < input.length(); ++i){
         char c = input[i];
         if(c == '+' || c == '*' || c == '-'){
             vector<int> leftRet = diffWaysToCompute(input.substr(0,i));
             vector<int> rightRet = diffWaysToCompute(input.substr(i+1));
             for(auto a : leftRet){
                 for(auto b : rightRet){
                     ret.push_back( c == '+' ? a+b : c == '-' ? a-b : a*b);
                 }
             }
         }
     }
     if(ret.empty()){
         ret.push_back(std::stoi(input));
     }
     return ret;
 }
 vector<int> diffWaysToCompute(string input) {
   vector<int> out;
   for (int i = 0; i < input.size(); i++) {
     char curChar = input[i];
     if (curChar == '+' || curChar == '-' || curChar == '*') {
       vector<int> result1 = diffWaysToCompute(input.substr(0, i));
       vector<int> result2 = diffWaysToCompute(input.substr(i + 1));
       for (int j = 0; j < result1.size(); j++) {
         for (int k = 0; k < result2.size(); k++) {
           if (curChar == '+')  out.push_back(result1[j] + result2[k]);
           if (curChar == '-')  out.push_back(result1[j] - result2[k]);
           if (curChar == '*')  out.push_back(result1[j] * result2[k]);
         }
       }
     }
   }
   if (out.empty())  out.push_back(stoi(input));
   return out;
 }
Exemplo n.º 20
0
int main()
{
    char *input = "2*3-4*5";

    int i, len, *ret;
    ret = diffWaysToCompute(input, &len);
    for (i = 0; i < len; i++) {
        printf("%d\n", ret[i]);
    }
    free(ret);
    return 0;
}
    vector<int> diffWaysToCompute(string input) {
        vector<int> res;

        for (int i = 0; i < input.size(); ++i)
        {
            if (input[i] == '+' || input[i] == '-' || input[i] == '*')
            {
                vector<int> left = diffWaysToCompute(input.substr(0, i));
                vector<int> right = diffWaysToCompute(input.substr(i+1));
                for (int j = 0; j < left.size(); ++j)
                {
                    for (int k = 0; k < right.size(); ++k)
                    {
                        res.push_back(compute(left[j], right[k], input[i]));
                    }
                }
            }
        }
        if (res.empty())
            res.push_back(std::stoi(input));
        return res;
    }
 vector<int> diffWaysToCompute(string input) {
     vector<int> ans;
     bool pureNum=true;
     for (int i=0; i<input.length(); i++) {
         char c = input[i];
         if (!isdigit(c)) {
             pureNum=false;
             vector<int> L=diffWaysToCompute(input.substr(0, i));
             vector<int> R=diffWaysToCompute(input.substr(i+1, input.length()-i-1));
             for (auto l : L) {
                 for (auto r : R) {
                     if (c=='+') ans.push_back(l+r);
                     else if (c=='-') ans.push_back(l-r);
                     else if (c=='*') ans.push_back(l*r);
                 }
             }
         }
     }
     if (pureNum)
         ans.push_back(atoi(input.c_str()));
     return ans;
 }
 vector<int> diffWaysToCompute(string input) 
 {
     vector<int>results;
     vector<int>vals1;
     vector<int>vals2;
     for (int i=0; i<input.size(); i++)
     {
         if (input[i]!='+' && input[i]!='-' && input[i]!='*')
             continue;
         vals1 = diffWaysToCompute(input.substr(0,i));
         vals2 = diffWaysToCompute(input.substr(i+1));
         for (int a : vals1)
             for (int b: vals2)
             {
                 if (input[i]=='+') results.push_back(a+b);
                 else if (input[i]=='-') results.push_back(a-b);
                 else if (input[i]=='*') results.push_back(a*b);
             }
     }
     if (results.empty())
         results.push_back(stoi(input));
     return results;
 }
int main() {
	srand(time(NULL));

    string s;
    while( cin>>s){
    vector<int> ans = diffWaysToCompute(s);
    printVector(ans);
    }




    return 0;
}
 vector<int> diffWaysToCompute(string input) {
     vector<int> ret;
     int size = input.size();
     for(int i =0;i<size;i++){
         char c = input[i];
         if(c =='+'||c =='-'||c=='*'){
             vector<int> ret1 = diffWaysToCompute(input.substr(0,i));
             vector<int> ret2 = diffWaysToCompute(input.substr(i+1));
             for(auto n1:ret1){
                 for(auto n2:ret2){
                     if(c=='+') ret.push_back(n1+n2);
                     else if (c=='-') ret.push_back(n1-n2);
                     else ret.push_back(n1*n2);
                 }
             }
         }
     }
     if(ret.empty())
     {
         ret.push_back(atoi(input.c_str()));
     }
     return ret;
 }
Exemplo n.º 26
0
 vector<int> diffWaysToCompute(string input) {
     vector<int> result;
     
     int len = input.length();
     if(len == 0){
         return result;
     }
     
     if(isInt(input)){
         result.push_back(std::stoi(input));
         return result;
     }
     
     for(int i=0; i<len; i++){
         if(input[i]<'0' || input[i]>'9'){
             vector<int> leftResult = diffWaysToCompute(input.substr(0, i));
             vector<int> rightResult = diffWaysToCompute(input.substr(i+1));
             for(int m = 0; m<leftResult.size(); m++){
                 for(int n = 0; n<rightResult.size(); n++){
                     switch(input[i]){
                         case '+':
                             result.push_back(leftResult[m] + rightResult[n]);
                             break;
                         case '-':
                             result.push_back(leftResult[m] - rightResult[n]);
                             break;
                         case '*':
                             result.push_back(leftResult[m] * rightResult[n]);
                             break;
                     }
                 }
             }
         }
     }
     
     return result;
 }
Exemplo n.º 27
0
    vector<int> diffWaysToCompute(string input) {
        int val=0,i=0;

        // only number exist in the input base case
        for(; i<input.size(); i++){
            if(isdigit(input[i])){
                val=val*10+input[i]-'0';
            }else{
                break;
            }
        }
        
        if(i==input.size()){
            return {val};
        }
        
        // split two parts with every operator
        // divide and conquer
        vector<int> res,leftres,rightres;
        
        for(int k=0; k<input.size(); k++){
            if(!isdigit(input[k])){
                //split using input[k]
                leftres=diffWaysToCompute(input.substr(0,k));
                rightres=diffWaysToCompute(input.substr(k+1));
                
                for(int i=0; i<leftres.size(); i++){
                    for(int j=0; j<rightres.size(); j++){
                        res.push_back(compute(leftres[i],rightres[j],input[k]));
                    }
                }
            }
        }
        
        return res;
        
    }
 vector<int> diffWaysToCompute(string input) {
     unordered_map<string, vector<int> > cache;
     return diffWaysToCompute(input, cache);
 }