dp|拆分控制

lc472

只尝试 `len < n` 确保由"更短单词"拼成;等长词不会参与

// 思路(排序+逐词断词 DP,长度去重优化)

// - 按长度升序处理,只用"更短"的词去拼当前词。

// - 对每个词做 word-break:`dpi` 表示前 i 位可由更短词拼出;只尝试出现过的"长度集合",剪枝快。

// - 复杂度:总 O(∑L · |lenSet|);因每词长度 ≤ 30、lenSet ≤ 30,且总长度 ≤ 1e5,足够快。

class Solution {

public:

std::vector<std::string> findAllConcatenatedWordsInADict(std::vector<std::string>& w)

{

std::sort(w.begin(), w.end(), \[\](auto& a, auto& b){ return a.size() < b.size(); });

std::unordered_set<std::string> S;

std::unordered_set<int> seenLen;

std::vector<int> L; // 已出现的不同长度

std::vector<std::string> ans;

for (auto& s : w) {

int n = s.size();

if (!S.empty() && ok(s, S, L)) ans.push_back(s);

S.insert(s);

if (seenLen.insert(n).second) L.push_back(n);

}

return ans;

}

private:

static bool ok(const std::string& s, const std::unordered_set<std::string>& S, const std::vector<int>& L)

{

int n = s.size();

std::vector<char> dp(n + 1); dp0 = 1;

for (int i = 0; i < n; ++i) if (dpi) {

for (int len : L) if (len < n && i + len <= n && S.count(s.substr(i, len)))

{

if ((dpi + len = 1) && dpn) return true;

}

}

return dpn;

}

};

lc466

找出由n1个s1拼接成的字符串里,++最多能包含多少个完整的s2(再除以n2得最终结果)++

过程中用循环检测来避免重复计算,提升效率

class Solution

{

//s1*n1删除某些字符能得到s2*n2*m

public:

int getMaxRepetitions(std::string s1, int n1, std::string s2, int n2) {

using ll = long long;

// 不可匹配的快速判零

std::array<int,26> has{}; for(char c: s1) hasc-'a'=1;

for(char c: s2) if(!hasc-'a') return 0;

int L2 = s2.size(), j = 0;

std::vector<int> first(L2, -1);

std::vector<ll> cnt(L2, 0);

ll c2 = 0; // 已匹配完整 s2 的次数

for (ll i = 0; i < n1; ++i) {

for (char c : s1) {

if (c == s2j && ++j == L2) j = 0, ++c2;

}

if (firstj != -1) { // 发现循环

ll i0 = firstj, c20 = cntj;

ll perBlk = i - i0, perCnt = c2 - c20;

ll loops = (n1 - 1 - i) / perBlk;

c2 += loops * perCnt;

i += loops * perBlk;

} else {

firstj = i; cntj = c2;

}

}

return (int)(c2 / n2);

}

};

lc791

直接返回 true / false 确保"在order中的字符优先排前面"

class Solution {

public:

string customSortString(string order, string s)

{

unordered_map<char,int> hash;

for(int i=0;i<order.size();i++)

{

hashorder\[i]=i;

}

sort(s.begin(),s.end(),\&(char& a,char& b)

{

if(hash.count(a) && hash.count(b))

return hasha<hashb;

else if(hash.count(a))

return true;

else if(hash.count(b))

return false;

else

return a<b;

});

return s;

}

};

相关推荐
D e re k ZH2 分钟前
保序回归:从 PAVA 到可并堆的单调约束优化
c++·算法·数据挖掘·回归·kotlin
青少儿编程课堂3 分钟前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:字典树前缀树与单词检索统计
c++·python·算法·bfs·信息学竞赛
octopus_c1 小时前
C++ 运算符重载详解 && 日期类的实现
开发语言·c++
青少儿编程课堂1 小时前
2026蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:堆排序与优先队列解析
c++·python·算法·bfs·信息学竞赛
j7~1 小时前
【C++ 面试笔记】(篇二):类与数据抽象 & STL 容器与算法
开发语言·c++·面试·stl容器和算法·类和数据抽象
Fuyo_11191 小时前
深入解析 C++ unordered_set 与 unordered_map 的核心特性与性能实战
开发语言·c++
码匠许师傅1 小时前
【C++三方组件】FFmpeg:音视频处理的瑞士军刀
c++·ffmpeg·音视频
ShineWinsu2 小时前
对于Qt:QPushButton常见属性的解析
开发语言·c++·qt·api·gui·按钮·qpushbutton
Lhan.zzZ2 小时前
用 Qt 构建线程安全的通讯静态库:从线程亲和性到完整工程
c++·vscode·qt·tcp/ip·udp
xiaoye-duck2 小时前
《C++ 项目:手写大模型接入 SDK》DeepSeek接口设计(上):初始化和全量响应
开发语言·网络·c++·语言模型