-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
old-yan
committed
Jul 7, 2024
1 parent
eaf0421
commit cd0b24d
Showing
24 changed files
with
158 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "IO/LeetcodeIO.h" | ||
#include "STR/ACAutomaton.h" | ||
using namespace std; | ||
|
||
/* | ||
[3213. 最小代价构造字符串](https://leetcode.cn/problems/construct-string-with-minimum-cost/) | ||
*/ | ||
/** | ||
* 本题通过 ac 自动机搜索所有适配位置 | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int minimumCost(string target, vector<string> &words, vector<int> &costs) { | ||
constexpr uint32_t inf = 0x3f3f3f3f; | ||
struct info { | ||
uint32_t real_fail; | ||
uint16_t len, cost = 0x3f3f; | ||
}; | ||
static OY::AC::Automaton<info, 26> S; | ||
S.reserve(50000); | ||
for (int i = 0; i < words.size(); i++) { | ||
auto p = S.get_node(S.insert_lower(words[i])); | ||
p->len = words[i].size(); | ||
p->cost = min<int>(p->cost, costs[i]); | ||
} | ||
S.prepare(); | ||
// 为了跳过没有匹配的结点,所以预处理一下 real_fail | ||
S.do_for_extending_nodes([](uint32_t cur) { | ||
auto p = S.get_node(cur); | ||
auto fa = S.get_fail_node(cur); | ||
p->real_fail = fa->len ? p->m_fail : fa->real_fail; | ||
}); | ||
vector<uint32_t> dp(target.size() + 1, inf); | ||
dp[0] = 0; | ||
uint32_t pos = 0; | ||
for (int r = 0; r < target.size(); r++) { | ||
pos = S.next(pos, target[r] - 'a'); | ||
// 遍历所有和 target[~r] 后缀相同的模式串 | ||
for (auto x = pos; x; x = S.get_node(x)->real_fail) { | ||
auto p = S.get_node(x); | ||
dp[r + 1] = min(dp[r + 1], dp[r - p->len + 1] + p->cost); | ||
} | ||
} | ||
return dp.back() == inf ? -1 : dp.back(); | ||
} | ||
}; | ||
|
||
#ifdef OY_LOCAL | ||
int main() { | ||
REGISTER_CONSTRUCTOR_SOLUTION; | ||
REGISTER_MEMBERFUNCTION_SOLUTION(minimumCost); | ||
while (true) { | ||
executor.constructSolution(); | ||
executor.executeSolution(); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "DS/LinkBucket.h" | ||
#include "DS/SegCounter.h" | ||
#include "IO/FastIO.h" | ||
#include "STR/ACAutomaton.h" | ||
|
||
/* | ||
[parent 树上启发式合并](https://ac.nowcoder.com/acm/problem/274852) | ||
*/ | ||
/** | ||
* 注意本题题面坑。询问字符串的和是 300000 | ||
*/ | ||
|
||
int main() { | ||
uint32_t n, q; | ||
std::string s; | ||
cin >> n >> q >> s; | ||
|
||
auto get_val = [](char c) { | ||
if (c <= '9') | ||
return c - '0'; | ||
else if (c <= 'Z') | ||
return c - 'A' + 10; | ||
else | ||
return c - 'a' + 36; | ||
}; | ||
OY::AC::Automaton<OY::AC::BaseNode, 62> ac; | ||
OY::LBC::LinkBucket<std::pair<uint32_t, uint32_t>> qs(300001, q); | ||
OY::StaticSegCounter<uint32_t, uint32_t, true, false, 200000> tails[300001]; | ||
for (uint32_t i = 0; i != q; i++) { | ||
std::string s; | ||
uint32_t k; | ||
cin >> s >> k; | ||
auto it = ac.insert(s.begin(), s.end(), [&](char c) { return get_val(c); }); | ||
qs[it].push_front(std::make_pair(i, k - 1)); | ||
} | ||
ac.prepare(); | ||
|
||
uint32_t pos = 0; | ||
for (uint32_t i = 0; i < s.size(); i++) { | ||
pos = ac.next(pos, get_val(s[i])); | ||
tails[pos].add_positive(i, 1); | ||
} | ||
std::vector<uint32_t> ans(q); | ||
ac.do_for_failing_nodes([&](uint32_t a) { | ||
// 先解决本地的询问 | ||
for (auto &[idx, k] : qs[a]) ans[idx] = k < tails[a].query_all() ? tails[a].kth(k)->key() + 1 : -1; | ||
// 再把本地的尾下标们合并到 fail 上 | ||
uint32_t p = ac.query_fail(a); | ||
tails[p].merge(tails[a]); | ||
}); | ||
// 输出答案 | ||
for (int a : ans) cout << a << endl; | ||
} |
Oops, something went wrong.