add: 230927
This commit is contained in:
parent
9c9e1f4604
commit
40036c20dc
|
|
@ -6,32 +6,32 @@
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
|
|
||||||
std::string LC230927::decodeAtIndex(const std::string& s, int k) {
|
std::string LC230927::decodeAtIndex(const std::string& s, int k) {
|
||||||
std::vector<std::tuple<std::string, int, unsigned long long>> col;
|
std::tuple<int, int, unsigned long long> col[51];
|
||||||
|
int level = 0;
|
||||||
unsigned long long len = 0;
|
unsigned long long len = 0;
|
||||||
std::string buf;
|
int begin = 0, end = 0;
|
||||||
for (char ch : s) {
|
for (char ch : s) {
|
||||||
if (std::isalpha(ch)) {
|
if (std::isalpha(ch)) {
|
||||||
++len;
|
++len;
|
||||||
buf += ch;
|
++end;
|
||||||
} else { // num
|
} else { // num
|
||||||
col.emplace_back(buf, ch ^ 0x30, len *= (ch ^ 0x30));
|
col[level++] = {begin, ch ^ 0x30, len *= (ch ^ 0x30)};
|
||||||
buf.clear();
|
begin = end = end + 1;
|
||||||
}
|
}
|
||||||
if (len >= k)
|
if (len >= k)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!buf.empty())
|
if (begin < end)
|
||||||
col.emplace_back(buf, 1, len);
|
col[level++] = {begin, 1, len};
|
||||||
const int level = col.size();
|
|
||||||
int ck = k - 1;
|
int ck = k - 1;
|
||||||
for (int i = level - 1; i >= 0; --i) {
|
for (int i = level - 1; i >= 0; --i) {
|
||||||
const auto prev_len = i ? std::get<2>(col[i - 1]) : 0ULL;
|
const auto prev_len = i ? std::get<2>(col[i - 1]) : 0ULL;
|
||||||
const auto& [c_str, c_dup, cur_len] = col[i];
|
const auto& [b, c_dup, cur_len] = col[i];
|
||||||
ck %= (cur_len / c_dup);
|
ck %= (cur_len / c_dup);
|
||||||
if (ck >= prev_len)
|
if (ck >= prev_len)
|
||||||
return {c_str[ck - prev_len]};
|
return {s[ck - prev_len + b]};
|
||||||
}
|
}
|
||||||
return {std::get<0>(col[0])[ck]};
|
return {s[ck]};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue