From 4fcc7edb67aeed4cf8eca7619d2534f97accb883 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Thu, 17 Mar 2022 21:48:40 +0800 Subject: [PATCH] add: 220317-CN [cpp] --- cpp/2203/220317-CN.cpp | 39 +++++++++++++++++++++++++++++++++++++++ cpp/2203/CMakeLists.txt | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 cpp/2203/220317-CN.cpp diff --git a/cpp/2203/220317-CN.cpp b/cpp/2203/220317-CN.cpp new file mode 100644 index 0000000..2286085 --- /dev/null +++ b/cpp/2203/220317-CN.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +class Solution { +public: + static std::string longestWord(const std::vector& words) { + std::unordered_set s(words.begin(), words.end()); + std::string ans = ""; + int ansLen = 0; + + std::queue q; + q.push(""); + while (!q.empty()) { + std::string x = q.front(); + q.pop(); + int xLen = x.length(); + for (char c = 'a'; c <= 'z'; ++c) { + std::string nx = x + c; + if (s.count(nx)) { + if (nx.length() > ansLen) { + ans = nx; + ansLen = nx.length(); + } + q.push(nx); + } + } + } + + return ans; + } +}; + +int main() { + std::cout << Solution::longestWord({"a","banana","app","appl","ap","apply","apple"}); + return 0; +} diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index 44a1aae..0630a05 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220317.cpp) +ADD_EXECUTABLE(2203 220317-CN.cpp)