樹狀結構儲存字串,查詢快,判斷字首快。
又稱單詞查詢樹,trie樹,是一種樹形結構,是一種雜湊樹的變種。
例題: link.
**(指標)
#include
#include
#include
#include <
set>
#include
#include
#include
// #include
#include
#include
#include
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define t int t
;scanf
("%d",&
t);while(t
--)const ll mod=
1e6+3;
const int maxn =
1e5+5;
struct trie
;trie *root;
//根結點不放字母
void
init
(trie *p)
void
insert
(string s)
cnt = cnt-
>nxt[id]
;//更新節點位置
cnt-
>num++
;//字首+1}}
int find
(string s)
return ans;
}int main()
while
(cin >> s)
return0;
}/*7bababa
band
beeabsolute
absord
acmbeer
*/
**(陣列)#include
#include
#include
#include <
set>
#include
#include
#include
#include
// #include
#include
#include
#include
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define t int t
;scanf
("%d",&
t);while(t
--)const ll mod=
1e9+7;
const int maxn =
2e6+5;
int trie[maxn][30
];int sum[maxn]
;int cnt;
int n,k;
void
insert
(string s)
sum[trie[root]
[id]]++
; root=trie[root]
[id];}
// vis[root]=1;
}int find
(string s)
return ans;
// return !vis[root];
}int main()
while
(cin >> s)
return0;
}/*5 6 2
1 1 2 5 2
5 6 2
1 1 1 5 2
*/
字典樹入門
字典樹終於稍微勉強地入了門。推薦部落格題目 練習題 講一下藍書的乙個例題 remember the word la3942 建議白天去牛客提交 題意 給你n個單詞,再給你乙個字元長串,問你有多少種分解方案。dp 字典樹 設dp i 表示 i n 內字串的分解方案數 相當於乙個字尾 我們列舉 n 1 ...
字典樹入門
今天先寫了01字典樹,學習部落格 這個01字典樹還是很簡單的,看看模板就會了 貼一下我的模板 const int maxn 1e5 10 int n,m,tot int trie 32 maxn 2 num maxn ll val maxn 32 void init void insert ll x...
字典樹入門
推薦部落格 之所以稱其為字典樹,是因為它在使用的過程中就是乙個類似查字典的過程,我們從第乙個字母乙個乙個的往下面去找 關於字典樹的寫法有兩種,一種是用鍊錶的形式去寫,比較好懂,還有一種就是用陣列的形式去寫,通過結點的編號實現相連的過程。int ch 200010 27 節點編號 int sz 字典樹...