add: 220517-CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-18 00:14:57 +08:00
parent 6f93cb693e
commit c0848960a6
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 32 additions and 1 deletions

31
cpp/2205/220517-CN.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <vector>
#include <string>
#include <cstdio>
class Solution {
public:
static bool isAlienSorted(const std::vector<std::string>& 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;
}

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220516-CN.cpp) ADD_EXECUTABLE(2205 220517-CN.cpp)