From c0848960a6955aa1919230e96e66d2977b6d3787 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Wed, 18 May 2022 00:14:57 +0800 Subject: [PATCH] add: 220517-CN [cpp] --- cpp/2205/220517-CN.cpp | 31 +++++++++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220517-CN.cpp diff --git a/cpp/2205/220517-CN.cpp b/cpp/2205/220517-CN.cpp new file mode 100644 index 0000000..81b1a82 --- /dev/null +++ b/cpp/2205/220517-CN.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +class Solution { +public: + static bool isAlienSorted(const std::vector& words, const std::string& order) { + int dict[256]{}; + for (int i = 0; i < order.length(); ++i) { + dict[order[i]] = i; + } + + auto strcmp = [&](const std::string& x, const std::string& y) { + const int xl = x.length(), yl = y.length(), kl = std::min(xl, yl); + for (int i = 0; i < kl; ++i) + if (dict[x[i]] - dict[y[i]]) + return dict[x[i]] - dict[y[i]]; + return xl - yl; + }; + + for (int i = 1; i < words.size(); ++i) + if (strcmp(words[i - 1], words[i]) > 0) + return false; + return true; + } +}; + +int main() { + std::printf("%s\n", Solution::isAlienSorted({"hello", "leetcode"}, "hlabcdefgijkmnopqrstuvwxyz") ? "yes" : "no"); + return 0; +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index f611761..070d48d 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220516-CN.cpp) +ADD_EXECUTABLE(2205 220517-CN.cpp)