Leetcode Algorithm 399. Evaluate Division
Evaluate Division
给定一系列形如A/B=k的等式,其中A,B是字符串类型的变量,k是一个实数,求某个新算式A’/B’的值。如果值不存在,则返回-1
解题思路
先看看题目给出的样例:
equations = [ ["a", "b"], ["b", "c"] ]
values = [2.0, 3.0]
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]
从给出的算式中我们可以直接得出什么信息呢?
a/b = 2, b/a = 1/2,
b/c = 3, c/b = 1/3,
a/a = 1, b/b = 1, c/c = 1,
间接得出什么信息呢?
a/c = 6, c/a = 1/6
总结起来,查询的结果有一下几种情况:
- 直接得出的信息,值存在;
- 间接得出的信息,值存在;
- 其它情况(无法间接推出/包含等式以外的变量),值不存在。
那么,间接推出到底是什么样的状况呢?我们来看看一下的例子:
ac=ab×bc
其实就是一个链式法则,也就是说,假如我们能够从a开始找到一条到达c的路径,那么这个值必然存在。
同样的,那些直接推出的式子也可以表达成一条路径,只不过它们不必展开成一条长链。而对于类似a/a=1的式子,路径的长度可以定义为0。
这样,我们就把这个问题转化为在一个无向图中寻找两点之间的路径的问题了。
对于无权重标注的无向图来说,最简单的路径寻找方法就是BSF或者DFS。但是我们想这条链尽量的短,因此采用BSF较为合适。
当我们搜寻路径的过程中,要把当前节点的前导节点记录下来才可以还原路径上经过的点。方法是将点放进队列时,记录该节点的前导节点即可。
代码
#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include <iomanip>
using namespace std;
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string> > equations,
vector<double>& values, vector<pair<string, string> > queries) {
map<string, vector<string> > graph;
map<pair<string, string>, double> value_map;
map<string, bool> visited;
vector<double> result;
// preprocess the graph and basic values
for (int i = 0; i < equations.size(); i++) {
string a = equations[i].first;
string b = equations[i].second;
graph[a].push_back(b);
graph[b].push_back(a);
value_map[make_pair(a, b)] = values[i];
value_map[make_pair(b, a)] = 1 / values[i];
visited[a] = 0;
visited[b] = 0;
}
// handle queries
for (int i = 0; i < queries.size(); i++) {
string a = queries[i].first;
string b = queries[i].second;
map<string, string> pre;
queue<string> q;
q.push(a);
pre[a] = ""; // pre-node of the root is NULL
bool found = false; // if the query is satisfied
// clear visited map
map<string, bool>::iterator iter;
for (iter = visited.begin(); iter != visited.end(); iter++) {
iter->second = 0;
}
// BFS search
while (!q.empty() && !found) {
string front = q.front();
// check whether the variable is in the graph
if (graph.find(front) != graph.end()) {
// reflexive break
if (front == b) {
found = true;
break;
}
visited[front] = true;
vector<string> v = graph[front];
for (int j = 0; j < v.size(); j++) {
// satisfied break
if (v[j] == b) {
pre[v[j]] = front;
found = true;
break;
}
if (!visited[v[j]]) {
q.push(v[j]);
pre[v[j]] = front;
}
}
}
q.pop();
}
if (found) {
string temp_a = pre[b];
string temp_b = b;
vector<pair<string, string> >::iterator it1, it2;
if (temp_a == "") { // root node
result.push_back(1);
} else {
double product = 1;
// find the path to the root
while (temp_a != "") {
product *= value_map[make_pair(temp_a, temp_b)];
// cout << temp_b << " ";
temp_b = temp_a;
temp_a = pre[temp_b];
}
// cout<< temp_b << endl;
result.push_back(product);
}
} else {
result.push_back(-1.0);
}
}
return result;
}
};
测试样例与输出
样例1
int main() {
vector<pair<string, string> > equations;
vector<double> values;
vector<pair<string, string> > queries;
equations.push_back(pair<string, string>("a", "b"));
equations.push_back(pair<string, string>("b", "c"));
values.push_back(2.0);
values.push_back(3.0);
queries.push_back(pair<string, string>("a", "c"));
queries.push_back(pair<string, string>("b", "a"));
queries.push_back(pair<string, string>("a", "e"));
queries.push_back(pair<string, string>("a", "a"));
queries.push_back(pair<string, string>("x", "x"));
Solution s;
vector<double> result = s.calcEquation(equations, values, queries);
for (int i = 0; i < result.size(); i++) {
cout.setf(ios::fixed);
cout << fixed << setprecision(5) << result[i] << endl;
}
return 0;
}
输出1
6.00000
0.50000
-1.00000
1.00000
-1.00000
样例2
int main() {
vector<pair<string, string> > equations;
vector<double> values;
vector<pair<string, string> > queries;
equations.push_back(pair<string, string>("a", "b"));
equations.push_back(pair<string, string>("e", "f"));
equations.push_back(pair<string, string>("b", "e"));
values.push_back(3.4);
values.push_back(1.4);
values.push_back(2.3);
queries.push_back(pair<string, string>("a", "f"));
queries.push_back(pair<string, string>("f", "e"));
queries.push_back(pair<string, string>("a", "a"));
Solution s;
vector<double> result = s.calcEquation(equations, values, queries);
for (int i = 0; i < result.size(); i++) {
cout.setf(ios::fixed);
cout << fixed << setprecision(5) << result[i] << endl;
}
return 0;
}
输出2
10.94800
0.71429
1.00000