add: 221024

This commit is contained in:
Eatswap 2022-10-24 13:08:16 +08:00
parent 5341fdda8f
commit a6dc9ecaec
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 61 additions and 1 deletions

60
cpp/2210/221024.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <string>
#include <vector>
#include <iostream>
/**
* 1239. Maximum Length of a Concatenated String with Unique Characters
*
* You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.
* Return the maximum possible length of s.
* A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
*/
class Solution {
private:
static int popcnt(uint32_t i) {
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = (i + (i >> 4)) & 0x0F0F0F0F;
return (i * 0x01010101) >> 24;
}
public:
static int maxLength(const std::vector<std::string>& arr) {
std::vector<int> s;
for (const auto& str : arr) {
int t = 0;
for (const char ch : str) {
if (t & (1 << ch - 'a')) {
t = -1;
break;
}
t |= 1 << ch - 'a';
}
if (t > 0)
s.push_back(t);
}
int n = s.size(), ret = 0, ret_pc = 0;
for (int i = 1 << n; --i; ) {
bool ok = true;
int cur = 0;
for (int j = 0; j < n; ++j) {
if (!(i & (1 << j)))
continue;
if (cur & s[j]) {
ok = false;
break;
}
cur |= s[j];
}
if (ok && ret_pc < popcnt(cur))
ret_pc = popcnt(ret = cur);
}
return ret_pc;
}
};
int main() {
std::cout << Solution::maxLength({"un", "iq", "ue"});
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2210)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2210 221024-CN.cpp) ADD_EXECUTABLE(2210 221024.cpp)