Evaluate Division

来源:互联网 发布:网络创业项目 编辑:程序博客网 时间:2024/09/21 08:15

Evaluate Division

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example:
Given a / b = 2.0, b / c = 3.0. 
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? . 
return [6.0, 0.5, -1.0, 1.0, -1.0 ].

The input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

According to the example above:

equations = [ ["a", "b"], ["b", "c"] ],values = [2.0, 3.0],queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. 

The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.

解析:

使用并查集的解法,若果一个等式的除数与被除数都没有出现,则建立一个集合的依赖关系,其中除数初始化为1,被除数为商,如果等式的两个数只有一个值在并查集中出现,则将另外一个值加入当前的并查集,若果两个值都出现过,需要合并两个并查集,合并并查集时其中一个并查集不变,另外一个并查集的每个数乘以一个因子进行合并。

代码:

class Solution {public:       unordered_map<string,string>father;      string findfather(string s)      {          while(father[s]!=s)          s=father[s];          return s;      }      void fatherUnion(unordered_map<string,double>&represent,unordered_map<string,string>&father,double r,string s1,string s2)      {          double ra=represent[s2]*r/represent[s1];          string f1=findfather(s1);          string f2=findfather(s2);          for (auto it=represent.begin(); it!=represent.end(); it++)          {              if (findfather(it->first)==f1)              {                  represent[it->first]=(it->second)*ra;              }          }                              father[s1]=s2;      }         vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {      unordered_map<string,double>represent;                 vector<double>ans;            for (int i=0; i<equations.size(); i++)      {                    double eq1,eq2;          int flag=0;          if (represent.find(equations[i].first)==represent.end())          {              flag++;          }          else          {              eq1=represent[equations[i].first];          }          if (represent.find(equations[i].second)==represent.end())          {              flag++;              eq2=1;          }          else          {              eq2=represent[equations[i].second];          }          if (flag==2)          {              represent[equations[i].second]=1;              represent[equations[i].first]=values[i];                            father[equations[i].second]=equations[i].second;              father[equations[i].first]=equations[i].second;                        }          else if (flag==1)          {               if (represent.find(equations[i].first)==represent.end())               {                   eq1=eq2*values[i];                   represent[equations[i].first]=eq1;                                      father[equations[i].first]=equations[i].second;               }               else               {                   eq2=eq1/values[i];                   represent[equations[i].second]=eq2;                   father[equations[i].second]=equations[i].first;               }          }          else          {                           fatherUnion(represent,father,values[i],equations[i].first,equations[i].second);          }                }      for (int i=0; i<queries.size(); i++)      {          double eq1,eq2;          if (represent.find(queries[i].first)==represent.end())          {              ans.push_back(-1.0);              continue;          }          else          {              eq1=represent[queries[i].first];          }                    if (represent.find(queries[i].second)==represent.end())          {              ans.push_back(-1.0);              continue;          }          else          {              eq2=represent[queries[i].second];          }                                        if (findfather(queries[i].first)==findfather(queries[i].second))           ans.push_back(eq1/eq2);           else           ans.push_back(-1.0);      }     return ans;                 }};



0 0
原创粉丝点击