From f87fc3bfc7631dea4bb82d83bca630e58c4c2008 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Mon, 13 Jun 2022 00:15:53 +0800 Subject: [PATCH] add: 220612-CN [cpp] --- cpp/2206/220612-CN.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ cpp/2206/CMakeLists.txt | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 cpp/2206/220612-CN.cpp diff --git a/cpp/2206/220612-CN.cpp b/cpp/2206/220612-CN.cpp new file mode 100644 index 0000000..b929b28 --- /dev/null +++ b/cpp/2206/220612-CN.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +/** + * 890. Find and Replace Pattern + * Given a list of strings words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order. + * A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. + * Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter. + */ + +class Solution { +private: + static unsigned long long hash(const std::string& str) { + unsigned long long ret = 0; + // char rm[27]{}; + int m[27]{}; // a -> 1. + int roundRobin = 0, pos = 1; + for (char ch : str) { + if (!m[ch - 'a' + 1]) { + // rm[pos] = ch; + m[ch - 'a' + 1] = pos++; + } + unsigned long long x = m[ch - 'a' + 1]; + ret ^= x << (roundRobin * 5); + roundRobin = (roundRobin + 1) % 12; + } + return ret; + } + +public: + static std::vector findAndReplacePattern(const std::vector& words, const std::string& pattern) { + // std::printf("Pattern -> 0x%016llX\n", hash(pattern)); + const auto patternHash = hash(pattern); + + std::vector ret; + for (const auto& str : words) { + // std::printf("%s -> 0x%016llX\n", str.c_str(), hash(str)); + if (patternHash == hash(str)) { + ret.push_back(str); + } + } + + return ret; + } +}; + +int main() { + Solution::findAndReplacePattern({"abc","deq","mee","aqq","dkd","ccc"}, "abb"); + return 0; +} diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index 2751e08..0990e20 100644 --- a/cpp/2206/CMakeLists.txt +++ b/cpp/2206/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2206) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2206 220606-CN.cpp) +ADD_EXECUTABLE(2206 220612-CN.cpp)