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.
解题思路:建立一个图,图的权值记为:如题目给出a/b = 2.0 则对于边(a,b)权值为2.0;对于边(b,a)权值为1/2.0。在获得到问题字符串对后,对图进行搜索,并从一开始记录权值,每前进一个元素,将此地与上一个节点的权值乘以上一个节点保存的从初始点到上一个节点的权值的乘积。在图搜索到目标字符串后,返回数值并结束搜索。
代码如下:
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <queue>
using namespace std;
struct Node {
string mark;
double distence;
};
bool search(vector<string> visited, string mark) {
bool in = false;
for (int i = 0; i < visited.size(); i++) {
if (visited[i] == mark)
in = true;
}
return in;
}
double findDistance(vector<list<Node> > graph, string from, string destination) {
queue<pair<string,double> > in;
vector<string> visited;
pair<string, double> temp;
temp.first = from;
temp.second = 1;
in.push(temp);
visited.push_back(from);
while (!in.empty()) {
// cout << "in" << endl;
pair<string, double> t = in.front();
// cout << "t:" << t.first<< " "<< t.second << endl;
in.pop();
bool have = false;
for (int i = 0; i < graph.size(); i++) {
if (graph[i].front().mark == t.first) {
if (from == destination) return 1;
have = true;
list<Node> ::iterator it = graph[i].begin();
it++;
for (it; it != graph[i].end(); it++) {
if (!search(visited, it->mark)) {
visited.push_back(it->mark);
// cout << it->mark << " :" << it->distence << " ";
pair<string, double> a;
a.first = it->mark;
a.second = it->distence * t.second;
// cout << a.second << endl;
in.push(a);
if (it->mark == destination) {
return a.second;
}
}
}
}
}
if (have == false) {
return -1;
}
}
return -1;
}
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string> > equations, vector<double>& values, vector<pair<string, string> > queries) {
vector<list<Node> > graph;
// cout << equations.size() << endl;
for (int i = 0; i < equations.size(); i++) {
// cout << "in" << endl;
string t1(equations[i].first);
string t2(equations[i].second);
bool had1 = false, had2 = false;
for (int j = 0; j < graph.size(); j++) {
if (graph[j].front().mark == t1) {
Node new1;
new1.mark = t2;
new1.distence = values[i];
graph[j].push_back(new1);
had1 = true;
}
if (graph[j].front().mark == t2) {
Node new1;
new1.mark = t1;
new1.distence = 1 / values[i];
graph[j].push_back(new1);
had2 = true;
}
}
if (had1 == false) {
list<Node> temp;
Node new1;
new1.mark = t1;
new1.distence = 1;
temp.push_back(new1);
graph.push_back(temp);
Node new2;
new2.mark = t2;
new2.distence = values[i];
graph[graph.size() - 1].push_back(new2);
}
if (had2 == false) {
list<Node> temp;
Node new1;
new1.mark = t2;
new1.distence = 1;
temp.push_back(new1);
graph.push_back(temp);
Node new2;
new2.mark = t1;
new2.distence = 1 / values[i];
graph[graph.size() - 1].push_back(new2);
}
}
/* for (int i = 0; i < graph.size(); i++) {
list<Node> ::iterator it = graph[i].begin();
cout << graph[i].front().mark << ": ";
it++;
for (int j = 1; j < graph[i].size(); j++) {
cout << it->mark << " -> " << it->distence << " ";
it++;
}
cout << endl;
}*/
vector<double> result;
for (int i = 0; i < queries.size(); i++) {
double b = findDistance(graph, queries[i].first, queries[i].second);
result.push_back(b);
}
return result;
}
};