Re: [閒聊] 每日leetcode

作者: oin1104 (是oin的說)   2025-01-07 22:22:58
題目
看甚麼字串是其他字串的子字串
思路
字典樹插入後綴時val++
查詢時如果val>1就代表有其他字的子字串是他
這個做法如果我可以O1插入後綴的話應該會比較快
但是後綴樹的那個演算法我不會
所以算了
```cpp
class TrieTree {
TrieTree* child[128];
public:
int val;
TrieTree(): val(0), child{0} { }
TrieTree* get(char c) {
if(!child[c]) child[c] = new TrieTree();
return child[c];
}
TrieTree* get(const string& s) {
TrieTree* t = this;
for(char c: s) {
t = t->get(c);
t->val ++;
}
return t;
}
TrieTree* find(char c) {
return child[c];
}
TrieTree* find(const string& s) {
TrieTree* t = this;
for(char c: s) {
t = t->find(c);
if(!t) break;
}
return t;
}
};
class Solution {
public:
vector<string> stringMatching(vector<string>& words)
{
TrieTree *save = new TrieTree();
int n = words.size();
vector<string> res;
for(int i = n-1 ; i >= 0 ; i
作者: mrsonic (typeB)   2025-01-07 22:25:00
臨摹圖不用一起發嗎?
作者: DJYOMIYAHINA (通通打死)   2025-01-07 22:28:00
大師
作者: Furina (芙寧娜)   2025-01-07 23:02:00
大師
作者: Meaverzt (Meaverzt)   2025-01-07 23:52:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com