From c78c6e93c82d605c1310d378d264b00de9e0b32c Mon Sep 17 00:00:00 2001 From: eat-swap Date: Sat, 30 Apr 2022 23:52:22 +0800 Subject: [PATCH] add: 220430 [cpp] --- cpp/2204/220430.cpp | 92 +++++++++++++++++++++++++++++++++++++++++ cpp/2204/CMakeLists.txt | 2 +- 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 cpp/2204/220430.cpp diff --git a/cpp/2204/220430.cpp b/cpp/2204/220430.cpp new file mode 100644 index 0000000..4f7b7a8 --- /dev/null +++ b/cpp/2204/220430.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include + +/** + * 399. Evaluate Division + * You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable. + * You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?. + * Return the answers to all queries. If a single answer cannot be determined, return -1.0. + */ + +class Solution { +public: + static std::vector calcEquation(const std::vector>& equations, const std::vector& values, const std::vector>& queries) { + int nodeCnt = 0; + std::unordered_map strId; + + // Give strings IDs + const int n = equations.size(); + for (int i = 0; i < n; ++i) { + // Place them into map, if not present + if (!strId.count(equations[i][0])) + strId[equations[i][0]] = nodeCnt++; + if (!strId.count(equations[i][1])) + strId[equations[i][1]] = nodeCnt++; + } + + std::vector> G(nodeCnt); + // Process equations + for (int i = 0; i < n; ++i) { + const int fId = strId[equations[i][0]], tId = strId[equations[i][1]]; + const double val = values[i]; + G[tId][fId] = 1.0 / (G[fId][tId] = val); + } + + auto getVal = [&](int fromId, int toId) { + std::queue> q; + + q.push({fromId, 1.0}); + std::vector vis(nodeCnt); + vis[fromId] = true; + while (!q.empty()) { + const auto [node, val] = q.front(); + q.pop(); + + for (const auto& [t, v] : G[node]) { + auto nextVal = val * v; + if (t == toId) + return nextVal; + if (vis[t]) + continue; + vis[t] = true; + + // Cache edge for future use + G[fromId][t] = nextVal; + G[t][fromId] = 1.0 / nextVal; + + // Push into BFS queue + q.push({t, nextVal}); + } + } + + // Not found everywhere + return -1.0; + }; + + std::vector ret; + // Process queries + // current ID, current val + for (const auto& query : queries) { + const auto& from = query[0]; + const auto& to = query[1]; + + if (!strId.count(from) || !strId.count(to)) + ret.push_back(-1.0); + else if (from == to) + ret.push_back(1.0); + else + ret.push_back(getVal(strId[from], strId[to])); + } + return ret; + } +}; + +int main() { + auto ans = Solution::calcEquation({{"a","b"},{"b","c"},{"bc","cd"}}, {1.5,2.5,5.0}, {{"a","c"},{"c","b"},{"bc","cd"},{"cd","bc"}}); + for (auto x : ans) + std::cout << x << std::endl; + return 0; +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index d89f49e..3e78c3d 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220429-CN.cpp) +ADD_EXECUTABLE(2204 220430.cpp)