From a3828a09dea853541b0612060e931c1021c55d8d Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Sat, 6 Dec 2025 14:56:06 +0800 Subject: [PATCH 1/9] =?UTF-8?q?20251206=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 "exercise/LC3578_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC3578_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC3578_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..b9a31c2 --- /dev/null +++ "b/exercise/LC3578_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,122 @@ +#include +using namespace std; + +// 【题目】LC3578. 统计极差最大为 K 的分割方式数 +// 【难度】中等(难度分:2033) +// 【提交】2025.12.5 https://leetcode.cn/problems/count-partitions-with-max-min-difference-at-most-k/submissions/683015100 +// 【标签】队列、数组、动态规划、前缀和、滑动窗口、单调队列、第453场周赛 + +// 方法1:用滑动窗口实现的划分型DP(双端队列容器deque)(执行时间:116ms | 击败:25.00%) +class Solution { +public: + int countPartitions(vector& nums, int k) { + const int MOD = 1'000'000'007; + int n = nums.size(); + vector f(n + 1); + f[0] = 1; + deque min_q, max_q; + long long sum = 0; + int left = 0; + + for(int i = 0; i < n; i++){ + int x = nums[i]; + sum += f[i]; + while(min_q.size() && x <= nums[min_q.back()]) min_q.pop_back(); + min_q.emplace_back(i); + while(max_q.size() && x >= nums[max_q.back()]) max_q.pop_back(); + max_q.emplace_back(i); + + while(nums[max_q.front()] - nums[min_q.front()] > k){ + sum -= f[left]; + left++; + while(min_q.front() < left) min_q.pop_front(); + while(max_q.front() < left) max_q.pop_front(); + } + f[i + 1] = sum % MOD; + } + return f[n]; + } +}; + +// 方法2:用滑动窗口实现的划分型DP(基于红黑树实现的、不去重且自动排序的 multiset)(执行时间:276ms | 击败:20.00%) +class Solution { +public: + int countPartitions(vector& nums, int k) { + int n = nums.size(); + long long mod = 1'000'000'007; + vector dp(n + 1); + vector prefix(n + 1); + multiset cnt; + dp[0] = 1; + prefix[0] = 1; + int left = 0; + for(int i = 0; i < n; i++){ + cnt.emplace(nums[i]); + while(left < i && *cnt.rbegin() - *cnt.begin() > k){ + cnt.erase(cnt.find(nums[left])); + left++; + } + dp[i + 1] = (prefix[i] - (left > 0 ? prefix[left - 1] : 0) + mod) % mod; + prefix[i + 1] = (prefix[i] + dp[i + 1] % mod); + } + return dp[n]; + } +}; + +// 方法3:用滑动窗口实现的划分型DP(去重且自动排序的 map)(执行时间:306ms | 击败:15.71%) +class Solution { +public: + int countPartitions(vector& nums, int k) { + int n = nums.size(); + long long mod = 1'000'000'007; + vector dp(n + 1); + vector prefix(n + 1); + map cnt; + dp[0] = 1; + prefix[0] = 1; + int left = 0; + for(int i = 0; i < n; i++){ + cnt[nums[i]]++; + while(left < i && cnt.rbegin()->first - (*cnt.begin()).first > k){ + cnt[nums[left]]--; + if(cnt[nums[left]] == 0) cnt.erase(nums[left]); + left++; + } + dp[i + 1] = (prefix[i] - (left > 0 ? prefix[left - 1] : 0) + mod) % mod; + prefix[i + 1] = (prefix[i] + dp[i + 1] % mod); + } + return dp[n]; + } +}; + +// 方法4:在方法1基础上,用前缀和数组替代前缀和单变量,空间换时间(执行时间:95ms | 击败:27.14%) +class Solution { +public: + int countPartitions(vector& nums, int k) { + const int MOD = 1'000'000'007; + int n = nums.size(); + vector f(n + 1); + vector prefix(n + 1); + f[0] = 1; + prefix[0] = 1; + deque min_q, max_q; + int left = 0; + + for(int i = 0; i < n; i++){ + int x = nums[i]; + while(min_q.size() && x <= nums[min_q.back()]) min_q.pop_back(); + min_q.emplace_back(i); + while(max_q.size() && x >= nums[max_q.back()]) max_q.pop_back(); + max_q.emplace_back(i); + + while(nums[max_q.front()] - nums[min_q.front()] > k){ + left++; + while(min_q.front() < left) min_q.pop_front(); + while(max_q.front() < left) max_q.pop_front(); + } + f[i + 1] = (prefix[i] - (left > 0 ? prefix[left - 1] : 0)) % MOD; + prefix[i + 1] = prefix[i] + f[i + 1]; + } + return f[n]; + } +}; -- Gitee From 9a67bd3d36899a95ca360761ada992696c55fdb7 Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Sat, 6 Dec 2025 15:03:05 +0800 Subject: [PATCH 2/9] =?UTF-8?q?20251206=E7=AC=AC=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 "exercise/LGP4456_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LGP4456_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LGP4456_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..578e580 --- /dev/null +++ "b/exercise/LGP4456_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,110 @@ +#include "iostream" +#include "vector" +typedef long long ll; +using namespace std; + +// 【题目】LGP4456. 交错队列 +// 【难度】困难(难度分:2600以上) +// 【提交】2025.12.5 https://www.luogu.com.cn/record/251595242 +// 【标签】快速幂、矩阵快速幂、斐波那契转移矩阵、动态规划 + +ll mod; + +vector> mat_mul(vector>& A, vector>& B){ + int n = B.size(); + vector> res(n, vector(n, 0)); + for(int i = 0; i < n; i++){ + for(int k = 0; k < n; k++){ + for(int j = 0; j < n; j++){ + res[i][j] += A[i][k] * B[k][j]; + } + } + } + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + res[i][j] %= mod; + } + } + return res; +} + +vector> mat_pow(vector> M, ll p){ + ll n = M.size(); + vector> res(n, vector(n, 0)); + for(int i = 0; i < n; i++) res[i][i] = 1; + while(p){ + if(p & 1) res = mat_mul(res, M); + M = mat_mul(M, M); + p >>= 1; + } + return res; +} + +ll pow(ll a, ll n){ + ll res = 1; + while(n){ + if(n & 1) res = (res * a) % mod; + a = a * a % mod; + n >>= 1; + } + return res; +} + +int main(){ + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, a, b; + cin >> n >> a >> b >> mod; + int max_k = a + b; + int len = max_k + 1; + + // 预处理组合数 + vector> C(max_k + 1, vector(max_k + 1, 0)); + for(int i = 0; i <= max_k; i++){ + C[i][0] = C[i][i] = 1; + for(int j = 1; j < i; j++){ + C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; + } + } + + // 构建转移矩阵 + vector> M(2 * len, vector(2 * len, 0)); + for(int i = 0; i < len; i++) M[i][i] = 1; + for(int i = 0; i < len; i++){ + for(int j = 0; j <= i; j++){ + M[i][len + j] = C[i][j]; + } + } + for(int i = 0; i < len; i++) M[len + i][i] = 1; + + // 构建初始向量 T = [F[1], F[0]] + vector T(2 * len, 0); + for(int k = 0; k <= max_k; k++) T[k] = (k == 0) ? 2 : 1; + T[len] = 1; + + vector Fn(len, 0); // 存储 F[n][k] + if(n == 1) for(int k = 0; k <= max_k; k++) Fn[k] = T[k]; + else { + vector> M_pow = mat_pow(M, n - 1); + vector V(2 * len, 0); + for(int i = 0; i < 2 * len; i++){ + ll sum = 0; + for(int j = 0; j < 2 * len; j++){ + sum = (sum + M_pow[i][j] * T[j]) % mod; + } + V[i] = sum; + } + for(int k = 0; k <= max_k; k++) Fn[k] = V[k]; + } + + ll ans = 0; + for(int i = 0; i <= a; i++){ + ll coefficient = C[a][i] * pow(n % mod, a - i) % mod; + if(i & 1) coefficient = (mod - coefficient) % mod; + int k = b + i; + ans = (ans + coefficient * Fn[k]) % mod; + } + cout << ans << endl; + return 0; +} -- Gitee From 9b7f35b601acff21b37e233c1b749fe4915851f8 Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Sun, 7 Dec 2025 13:01:50 +0800 Subject: [PATCH 3/9] =?UTF-8?q?20251207=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "exercise/LC1523_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC1523_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC1523_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..dfb5a96 --- /dev/null +++ "b/exercise/LC1523_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,23 @@ +#include +using namespace std; + +// 【题目】LC1523. 在区间范围内统计奇数数目 +// 【难度】简单(难度分:1209) +// 【提交】2025.12.7 https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/submissions/683188088 +// 【标签】数学、第31场双周赛 + +// 方法1:【完全自己做】(执行时间:0ms | 击败:100.00%) +class Solution { +public: + int countOdds(int low, int high) { + return ((high - low) >> 1) + ((high & 1 || low & 1) ? 1 : 0); + } +}; + +// 方法2:【灵神】数学推导(执行时间:0ms | 击败:100.00%) +class Solution { +public: + int countOdds(int low, int high) { + return ((high + 1) >> 1) - (low >> 1); + } +}; -- Gitee From fa31529fec0c681d46c63ff5734fd9de19b911cc Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Sun, 7 Dec 2025 14:56:05 +0800 Subject: [PATCH 4/9] =?UTF-8?q?20251207=E7=AC=AC=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 42 ++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 134 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 "exercise/LC3769_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "exercise/LC3770_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC3769_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC3769_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..82ec8c2 --- /dev/null +++ "b/exercise/LC3769_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,42 @@ +#include +using namespace std; + +// 【题目】LC3769. 二进制反射排序 +// 【难度】简单 +// 【提交】2025.12.7 https://leetcode.cn/problems/sort-integers-by-binary-reflection/submissions/683194654 +// 【标签】第479场周赛 + +// 方法1:手写排序规则(执行时间:0ms | 击败:100.00%) +class Solution { +public: + vector sortByReflection(vector& nums) { + sort(nums.begin(), nums.end(), [](const int a, const int b) { + auto rnum = [](int num){ + int ret = 0; + while(num){ + ret = (ret << 1) + (num & 1); + num >>= 1; + } + return ret; + }; + int ra = rnum(a), rb = rnum(b); + if(ra != rb) return ra < rb; + else return a < b; + }); + return nums; + } +}; + +// 方法2:【灵神】二进制库函数(执行时间:0ms | 击败:100.00%) +class Solution { +public: + vector sortByReflection(vector& nums) { + ranges::sort(nums, {}, [&](int x) { + int rev = __builtin_bitreverse32(x) >> __builtin_clz((uint32_t)x); + return pair(rev, x); + }); + return nums; + } +}; + + diff --git "a/exercise/LC3770_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC3770_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..10ba613 --- /dev/null +++ "b/exercise/LC3770_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,134 @@ +#include +using namespace std; + +// 【题目】LC3770. 可表示为连续质数和的最大质数 +// 【难度】中等 +// 【提交】2025.12.7 https://leetcode.cn/problems/largest-prime-from-consecutive-prime-sum/submissions/683201737 +// 【标签】第479场周赛 + +// 方法1:【自己做】埃氏筛(执行时间:543ms | 击败:47.43%) +class Solution { +public: + int largestPrime(int n) { + if(n == 1) return 0; + if(n == 2) return 2; + vector is_prime(n + 1, true); + is_prime[0] = is_prime[1] = false; + for(int i = 2; i * i <= n; i++){ + if(is_prime[i]){ + for(int j = i * i; j <= n; j += i) is_prime[j] = false; + } + } + + int ans = 2; + int sum = 2; + for(int i = 3; i <= n; i++){ + if(is_prime[i]){ + if(sum + i > n) return ans; + sum += i; + if(is_prime[sum]) ans = sum; + } + } + return ans; + } +}; + +// 方法2:【自己做】欧拉筛(执行时间:1370ms | 击败:10.48%)【数据量过小(n <= 1e6)欧拉筛必慢于埃氏筛】 +class Solution { +public: + int largestPrime(int n) { + if(n == 1) return 0; + if(n == 2) return 2; + vector is_prime(n + 1, true); + vector primes; + is_prime[0] = is_prime[1] = false; + for(int i = 2; i <= n; i++){ + if(is_prime[i]) primes.emplace_back(i); + for(int j = 0; j < primes.size() && i * primes[j] <= n; j++){ + is_prime[i * primes[j]] = false; + if(i % primes[j] == 0) break; + } + } + + int ans = 2; + int sum = 2; + for(int i = 3; i <= n; i++){ + if(is_prime[i]){ + if(sum + i > n) return ans; + sum += i; + if(is_prime[sum]) ans = sum; + } + } + return ans; + } +}; + +// 方法3:【灵神】预处理 + 埃氏筛 + 前缀和 + 二分查找(执行时间:0ms | 击败:100.00%)【最优解】 +const int MX = 500'000; +bool is_prime[MX + 1]; +vector primes; +vector special_pimes = {0}; + +auto init = [] { + ranges::fill(is_prime, true); + is_prime[0] = is_prime[1] = false; + for(int i = 2; i <= MX; i++){ // 埃氏筛 + if(is_prime[i]){ + primes.emplace_back(i); + for(long long j = 1LL * i * i; j <= MX; j += i){ + is_prime[j] = false; + } + } + } + + int sum = 0; + for(int p : primes){ + sum += p; + if(sum > MX) break; + if(is_prime[sum]) special_pimes.emplace_back(sum); + } + return 0; +}(); + +class Solution { +public: + int largestPrime(int n) { + return *--upper_bound(special_pimes.begin(), special_pimes.end(), n); + } +}; + +// 方法4:【灵神】预处理 + 埃氏筛 + 前缀和 + 双指针(执行时间:0ms | 击败:100.00%) +const int MX = 500'000; +bool is_prime[MX + 1]; +vector primes; +int ans[MX + 1]; + +auto init = [] { + ranges::fill(is_prime, true); + is_prime[0] = is_prime[1] = false; + for(int i = 2; i <= MX; i++){ // 埃氏筛 + if(is_prime[i]){ + primes.emplace_back(i); + for(long long j = 1LL * i * i; j <= MX; j += i){ + is_prime[j] = false; + } + } + } + + int sum = 0; + for(int i = 2, j = 0; i <= MX; i++){ + if(sum + primes[j] <= i){ + sum += primes[j]; + j++; + } + ans[i] = is_prime[sum] ? sum : ans[i - 1]; + } + return 0; +}(); + +class Solution { +public: + int largestPrime(int n) { + return ans[n]; + } +}; -- Gitee From 59b01dcfc61f57dd31282ca7fe963c314b807db3 Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Mon, 8 Dec 2025 01:14:09 +0800 Subject: [PATCH 5/9] =?UTF-8?q?20251208=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 51 +++++++++++++++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 48 +++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 "exercise/LGP1481_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "exercise/LGP4824_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LGP1481_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LGP1481_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..a380861 --- /dev/null +++ "b/exercise/LGP1481_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,51 @@ +#include "iostream" +#include "algorithm" +using namespace std; + +// 【题目】LGP1481. 魔族密码 +// 【难度】困难 +// 【提交】2025.12.8 https://www.luogu.com.cn/record/252126070 +// 【标签】字典树Trie、树形DP + +const int MAXN = 75 * 2000 + 1; // 最大节点数:单词数 ×最大长度 +int trie[MAXN][26]; // Trie 树结构 +int dp[MAXN]; // 以该节点结尾的单词的最长词链长度(0 表示非单词节点) +int nodeCnt = 0; // 节点计数器 + +int main(){ + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + int N; + cin >> N; + int ans = 0; + string word; + for(int i = 0; i < N; i++){ + cin >> word; + + int p = 0; // 字典树的根节点 + int maxPrefix = 0; // 路径上前缀单词的最大链长 + + // 遍历单词字符 + for(char ch : word){ + int c = ch - 'a'; + if(trie[p][c] == 0) { + trie[p][c] = ++nodeCnt; + } + p = trie[p][c]; + // 若当前节点是单词结尾,更新最大前缀链长 + if(dp[p] > 0){ + maxPrefix = max(maxPrefix, dp[p]); + } + } + + // 当前单词的链长 = 最大前缀链长 + 1 + int curLen = maxPrefix + 1; + dp[p] = curLen; // 记录当前单词链长 + ans = max(ans, curLen); // 更新答案 + } + + cout << ans << endl; + return 0; +} + diff --git "a/exercise/LGP4824_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LGP4824_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..9a64412 --- /dev/null +++ "b/exercise/LGP4824_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,48 @@ +#include "iostream" +#include "iostream" +#include "vector" +#include "stack" +#include "string" +#include "algorithm" +using namespace std; + +// 【题目】LGP4824. [USACO15FEB] Censoring S +// 【难度】困难 +// 【提交】2025.12.8 https://www.luogu.com.cn/record/252112855 +// 【标签】KMP匹配算法、栈 + +int main(){ + string S, T; + cin >> S >> T; + + int m = T.size(); + vector pi(m); + for(int i = 1, j = 0; i < m; i++){ + while(j && T[i] != T[j]) j = pi[j - 1]; + if(T[i] == T[j]) j++; + pi[i] = j; + } + + stack> stk; + int n = S.length(); + int j = 0; + for(char c : S){ + while(j && c != T[j]) j = pi[j - 1]; + if(c == T[j]) j++; + stk.emplace(c, j); + if(j == m) { + for(int k = 0; k < m; k++) stk.pop(); + j = stk.empty() ? 0 : stk.top().second; + } + } + + string res; + while(!stk.empty()){ + res += stk.top().first; + stk.pop(); + } + reverse(res.begin(), res.end()); + cout << res << endl; + + return 0; +} -- Gitee From 7898baee53d64b0e38b9ab7c9ca782e941d1dbbf Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Tue, 9 Dec 2025 01:28:42 +0800 Subject: [PATCH 6/9] =?UTF-8?q?20251209=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 42 +++++++++++++++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 29 +++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 "exercise/LC1925_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "exercise/LC2223_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC1925_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC1925_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..26ac555 --- /dev/null +++ "b/exercise/LC1925_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,42 @@ +#include "bits/stdc++.h" +using namespace std; + +// 【题目】LC1925. 统计平方和三元组的数目 +// 【难度】简单 +// 【提交】2025.12.8 https://leetcode.cn/problems/count-square-sum-triples/submissions/683517534 +// 【标签】数学、枚举、第56场双周赛 + +// 方法1:三层循环暴力(执行用时:60ms | 击败:20.97%) +class Solution { +public: + int countTriples(int n) { + int res = 0; + for(int i = 3; i <= n; i++){ + for(int j = 2; j < i; j++){ + for(int k = 1; k < j; k++){ + if(j * j + k * k == i * i){ + res += 2; + } + } + } + } + return res; + } +}; + +// 方法2:二层循环(执行用时:0ms | 击败:100.00%) +class Solution { +public: + int countTriples(int n) { + int res = 0; + for(int i = 2; i <= n; i++){ + for(int j = 1; j < i; j++){ + int k = int(sqrt(i * i + j * j)); + if(k <= n && k * k == i * i + j * j){ + res += 2; + } + } + } + return res; + } +}; diff --git "a/exercise/LC2223_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC2223_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..2fb1d1d --- /dev/null +++ "b/exercise/LC2223_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,29 @@ +#include "bits/stdc++.h" +using namespace std; + +// 【题目】LC2223. 构造字符串的总得分和 +// 【难度】困难(难度分:2220) +// 【提交】2025.12.8 https://leetcode.cn/problems/sum-of-scores-of-built-strings/submissions/683559308 +// 【标签】字符串、二分查找、字符串匹配、后缀数组、哈希函数、滚动哈希、第75场双周赛 + +// 方法1:Z函数(执行用时:16ms | 击败:62.28%)【Z函数模板代码】 +class Solution { +public: + long long sumScores(string s) { + int n = s.length(); + vector Z(n); + Z[0] = n; + int l = 0, r = 0; + for(int i = 1; i < n; i++){ + if(i <= r){ + Z[i] = min(r - i + 1, Z[i - l]); + } + while(i + Z[i] < n && s[Z[i]] == s[i + Z[i]]) Z[i]++; + if(i + Z[i] - 1 > r){ + l = i; + r = i + Z[i] - 1; + } + } + return accumulate(Z.begin(), Z.end(), 0LL); + } +}; -- Gitee From c7fd8c9bb7ad22807e5c23c45525941c5ac81f32 Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Thu, 11 Dec 2025 01:34:09 +0800 Subject: [PATCH 7/9] =?UTF-8?q?20251211=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 40 +++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 73 ++++++++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 109 ++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 "exercise/LC3577_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "exercise/LC3583_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "exercise/LGP2536_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC3577_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC3577_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..2b0986f --- /dev/null +++ "b/exercise/LC3577_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,40 @@ +#include "bits/stdc++.h" +using namespace std; + +// 【题目】LC3577. 统计计算机解锁顺序排列数 +// 【难度】中等(难度分:1750) +// 【提交】2025.12.10 https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/submissions/683986429 +// 【标签】脑筋急转弯、数组、数学、组合数学、第453场周赛 + +// 方法1:【完全自己做】脑筋急转弯(执行用时:23ms | 击败:5.48%) +class Solution { + const int mod = 1'000'000'007; +public: + int countPermutations(vector& complexity) { + int n = complexity.size(); + sort(complexity.begin() + 1, complexity.end()); + if(complexity[1] <= complexity[0]) return 0; + long long res = 1; + n--; + while(n){ + res = (res * n) % mod; + n--; + } + return int(res); + } +}; + +// 方法1:【灵神】脑筋急转弯(执行用时:7ms | 击败:10.96%) +class Solution { + const int mod = 1'000'000'007; +public: + int countPermutations(vector& complexity) { + int n = complexity.size(); + long long ans = 1; + for(int i = 1; i < n; i++){ + if(complexity[i] <= complexity[0]) return 0; + ans = ans * i % mod; + } + return ans; + } +}; diff --git "a/exercise/LC3583_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC3583_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..7a55a5d --- /dev/null +++ "b/exercise/LC3583_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,73 @@ +#include "bits/stdc++.h" +using namespace std; + +// 【题目】LC3583. 统计特殊三元组 +// 【难度】中等(难度分:1510) +// 【提交】2025.12.8 https://leetcode.cn/problems/count-special-triplets/submissions/683797682 +// 【标签】数组、哈希表、计数、第454场周赛 + +// 方法1:枚举 + 计数(执行用时:1176ms | 击败:23.04%) +using ll = long long; +class Solution { + const int mod = 1'000'000'007; +public: + int specialTriplets(vector& nums) { + int n = nums.size(); + ll ans = 0; + unordered_map all_cnt, left_cnt; + for(int x : nums){ + all_cnt[x]++; + } + for(int x : nums){ + int target = 2 * x; + int left = left_cnt[target]; + left_cnt[x]++; + int right = all_cnt[target] - left_cnt[target]; + ans = (ans + 1LL * left * right) % mod; + } + return ans; + } +}; + +// 方法2:枚举 + 计数(执行用时:1093ms | 击败:41.87%) +using ll = long long; +class Solution { + const int mod = 1'000'000'007; +public: + int specialTriplets(vector& nums) { + int n = nums.size(); + ll ans = 0; + unordered_map suf, pre; + for(int x : nums){ + suf[x]++; + } + for(int x : nums){ + suf[x]--; + ans += 1LL * pre[x * 2] * suf[x * 2]; + pre[x]++; + } + return ans % mod; + } +}; + +// 方法3:枚举右,维护左(一次遍历)(执行用时:827ms | 击败:80.62%) +using ll = long long; +class Solution { + const int mod = 1'000'000'007; +public: + int specialTriplets(vector& nums) { + int n = nums.size(); + ll ans = 0; + unordered_map cnt1; + unordered_map cnt12; + + for(int x : nums){ + if((x & 1) == 0){ + ans += cnt12[x >> 1]; + } + cnt12[x] += cnt1[x * 2]; + cnt1[x]++; + } + return ans % mod; + } +}; diff --git "a/exercise/LGP2536_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LGP2536_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..f6b3bee --- /dev/null +++ "b/exercise/LGP2536_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,109 @@ +#include "bits/stdc++.h" +using namespace std; + +// 【题目】LGP2536. [AHOI2005] 病毒检测 +// 【难度】中等 +// 【提交】2025.12.10 https://www.luogu.com.cn/record/252528253 +// 【标签】字符串、字典树 Trie、AC自动机、各省省选 + +// 方法1:动态规划(执行用时:182ms) +const int MAXN = 1002; +bool dp[1002][502]; + +bool isMatch(const string& pattern, const string& s){ + int m = pattern.length(); + int n = s.length(); + memset(dp, false, sizeof(dp)); + dp[0][0] = true; + + for(int i = 1; i <= m; i++){ + if(pattern[i - 1] == '*') dp[i][0] = dp[i - 1][0]; + } + + for(int i = 1; i <= m; i++){ + for(int j = 1; j <= n; j++){ + if(pattern[i - 1] == '*') dp[i][j] = dp[i - 1][j] || dp[i][j - 1]; + else if(pattern[i - 1] == '?') dp[i][j] = dp[i - 1][j - 1]; + else { + if(pattern[i - 1] == s[j - 1]) dp[i][j] = dp[i - 1][j - 1]; + } + } + } + return dp[m][n]; +} + +int main(){ + string pattern; + cin >> pattern; + int N; + cin >> N; + + int res = 0; + for(int i = 0; i < N; i++){ + string DNA; + cin >> DNA; + if(!isMatch(pattern, DNA)) res++; + } + + cout << res << endl; + return 0; +} + + +// 方法2:记忆化搜索(执行用时:1670ms) +string pattern; +int memo[10002][502]; + +bool match_dfs(int pos_p, int pos_dna, const string& DNA){ + int& res = memo[pos_p][pos_dna]; + if(res != -1) return res; + + int m = pattern.length(); + int n = DNA.length(); + if(pos_p == m && pos_dna == n){ + memo[pos_p][pos_dna] = 1; + return true; + } + if(pos_p == m){ + memo[pos_p][pos_dna] = -1; + return false; + } + if(pos_dna == n){ + for(int i = pos_p; i < m; i++){ + if(pattern[i] != '*') { + memo[pos_p][pos_dna] = -1; + return false; + } + } + memo[pos_p][pos_dna] = 1; + return true; + } + + res = 0; + int cur = pattern[pos_p]; + if(cur == '*'){ + res = match_dfs(pos_p + 1, pos_dna, DNA); + if(!res) res = match_dfs(pos_p, pos_dna + 1, DNA); + } else if(cur == '?'){ + res = match_dfs(pos_p + 1, pos_dna + 1, DNA); + } else { + if(cur == DNA[pos_dna]) res = match_dfs(pos_p + 1, pos_dna + 1, DNA); + else res = false; + } + return res; +} + +int main(){ + cin >> pattern; + int N; + cin >> N; + int res = 0; + for(int i = 0; i < N; i++){ + string DNA; + cin >> DNA; + memset(memo, -1, sizeof(memo)); + if(!match_dfs(0, 0, DNA)) res++; + } + cout << res << endl; + return 0; +} -- Gitee From ea068cf7594a15ff1a450f1358299bfcb720ae09 Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Thu, 11 Dec 2025 12:20:05 +0800 Subject: [PATCH 8/9] =?UTF-8?q?20251211=E7=AC=AC=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 "exercise/LC3531_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC3531_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC3531_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..12449e6 --- /dev/null +++ "b/exercise/LC3531_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,38 @@ +#include "bits/stdc++.h" +using namespace std; + +// 【题目】LC3531. 统计被覆盖的建筑 +// 【难度】中等 (难度分:1519) +// 【提交】2025.12.11 https://leetcode.cn/problems/count-covered-buildings/submissions/684086088 +// 【标签】数组、哈希表、排序、第447场周赛 + +// 方法1:(执行用时:43ms | 击败:40.68%) +class Solution { +public: + int countCoveredBuildings(int n, vector>& buildings) { + vector col_min(n + 1, n + 1); + vector col_max(n + 1, 0); + vector row_min(n + 1, n + 1); + vector row_max(n + 1, 0); + for(auto& pos : buildings){ + int x = pos[0]; // 行 + int y = pos[1]; // 列 + col_min[y] = min(col_min[y], x); + col_max[y] = max(col_max[y], x); + row_min[x] = min(row_min[x], y); + row_max[x] = max(row_max[x], y); + } + int cnt = 0; + for(auto& pos : buildings){ + int x = pos[0]; + int y = pos[1]; + bool up = row_max[x] > y; + bool down = row_min[x] < y; + bool left = col_min[y] < x; + bool right = col_max[y] > x; + + if(up && down && left && right) cnt++; + } + return cnt; + } +}; -- Gitee From 66beefe0c9a9582f84c5bd0a02c4c87eaa55fee8 Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Fri, 12 Dec 2025 01:56:39 +0800 Subject: [PATCH 9/9] =?UTF-8?q?20251212=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 "exercise/LC3433_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC3433_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC3433_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..1014b59 --- /dev/null +++ "b/exercise/LC3433_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,41 @@ +#include "bits/stdc++.h" +using namespace std; + +// 【题目】LC3433. 统计用户被提及情况 +// 【难度】中等 (难度分:1745) +// 【提交】2025.12.12 https://leetcode.cn/problems/count-mentions-per-user/submissions/684260525 +// 【标签】数组、数学、排序、模拟、第434场周赛 + +// 方法1:按照时间戳排序 + 模拟(执行用时:45ms | 击败:7.03%) +class Solution { +public: + vector countMentions(int numberOfUsers, vector>& events) { + ranges::sort(events, {}, [](auto& e) { + return pair(stoi(e[1]), e[0][2]); + }); + + int m = events.size(); + vector res(numberOfUsers, 0); + vector online(numberOfUsers); + for(auto& e : events){ + int cur_t = stoi(e[1]); + string& mention = e[2]; + if(e[0][0] == 'O'){ // 离线 + online[stoi(mention)] = cur_t + 60; + } else if(mention[0] == 'A'){ + for(int& x : res) x++; + } else if(mention[0] == 'H') { + for(int i = 0; i < numberOfUsers; i++){ + if(online[i] <= cur_t) res[i]++; + } + } else { + stringstream ss(mention); + string part; + while(ss >> part){ + res[stoi(part.substr(2))]++; + } + } + } + return res; + } +}; -- Gitee