Professional Documents
Culture Documents
tv
Cây ti n t là m t c u trúc d li u s d ng cây có th t m t
m ng liên k t c a các xâu kí t . Tôi xin trình bày rõ v ch nt
II. N i dung
1.Trie là gì?
Trong , trie, hay câ
c . Không
Ví d Cây ti n t t
2
2.
:
m
O(m) O(logN) phép so
sánh xâu (N
O(m.logN)
Do khôn
M t ng d ng ph bi n c bi u di n t n. c bi t
phù h p cho các ng d i tìm ki m x p x , ch ng h n m m soát
l i chính t .
C++ Code:
1. class Trie {
2.
3. }
C++ Code:
1. struct node {
2. int num, index, w_count;
3. int child[26];
3
4. };
T
chi
C++ Code:
1. vector<node> tree;
2. inline void new_node() {
3. node aw;
4. aw.num = 0;
5. for (int i = 0; i < 10; i++) aw.child[i] = -1;
6. aw.index = tree.size();
7. tree.push_back(aw);
8. }
9. inline void destruct() {
10. tree.clear();
11. }
12.
13. inline void init() {
14. new_node();
15. }
C++ Code:
1. inline void add_word(const string& s) {
2. int w;
3. int indx = 0;
4. for (int i = 0; i < s.size(); i++) {
5. tree[indx].num++;
6. w = s[i] - 'a';
4
7. if (tree[indx].child[w] == -1) {
8. new_node();
9. tree[indx].child[w] = tree.size() - 1;
10. }
11. indx = tree[indx].child[w];
12. }
13. tree[indx].num++;
14. tree[indx].w_count++;
15. }
C++ Code:
1. inline int count_pref(const string& s) {
2. int w;
3. int indx = 0;
4. for (int i = 0; i < s.size(); i++) {
5. w = s[i] - 'a';
6. if (tree[indx].child[w] == -1) return 0;
7. indx = tree[indx].child[w];
8. }
9. return tree[indx].num;
10. }
4.5
C++ Code:
1. inline int count_word(const string& s) {
2. int w;
3. int indx = 0;
4. for (int i = 0; i < s.size(); i++) {
5. w = s[i] - 'a';
6. if (tree[indx].child[w] == -1) return 0;
7. indx = tree[indx].child[w];
8. }
9. return tree[indx].w_count;
5
10. }
4.6
C++ Code:
1. inline void del_word(const string& s) {
2. if (!count_pref(s)) return;
3. int w;
4. int indx = 0;
5. for (int i = 0; i < s.size(); i++) {
6. tree[indx].num--;
7. w = s[i] - 'a';
8. indx = tree[indx].child[w];
9. }
10. tree[indx].num--;
11. tree[indx].w_count--;
12. }
5.1 CHAIN2 -
1, w2, ..., wn
i i+1.
1, s2, ..., sm
:
6
3 5
a a
ab ab
abc bc
bcd
add
3 2
Thu t toán
M t trong nh ng cách gi i v i bài này là s d ng trie:
t c các t l iv im dài
chu i t dài nh t b u
t m t s ph n quan tr ng
type
trie = ^node;
node = record
f,u : longint; //S ng xâu k t thúc t i m t nút
n các nút con c a m t nút
end;
...
procedure dfs(a:trie);
var
c : char;
begin
a^.f:=0
for c:='a' to 'z' do
begin
dfs(a^.c[c]);
a^.f:=max(a^.f,a^.c[c].f);
end;;
7
inc(a^.f,a^.u);
end;
function solve:longint;
begin
add(root);
for i:=1 to m do insert(s[i]);
dfs(root);
exit(root^.f);
end;
5.2 Tách t
M tx n con sao cho m i
n con thu c m t t p g c, m i t
t nào gi ng nhau.
Vi nh s cách tách m t t c.
Thu t toán
Quy ho ng
t f[i] = s n t 1..i c a S.
y, f[i] = t ng f[j] v i m i j tho n t j+1..i là m t t thu c t p
t t tính f[i] v i i ch y t n n. V i m ki m tra m n
j..i có là m t t c không, chú ý là khi gi m j, các t
và t c là h u t c a t sau, các t
trên cây trie, ta có th g c xu ng các nút th hi n các xâu này, n
c n a, t c là không có t nào tho mãn. Chú ý là khi thêm các xâu c a t
cho, ta c n thêm các xâu này theo chi c (ho c m t cách x lý khác là ta tính
hàm f t n 1).
t m t s ph n quan tr ng
f[0]=1;
for i:=1 to n do
begin
j:=i; p:=root;
while (j>0) and (p^.c[s[j]]<>nil) do
8
begin
p:=p^.c[s[j]];
dec(j);
if (x^.c=1) then inc(f[i],f[j]);
end;
end;
9
Son
Yeu
- 911
-
- 91125426
Dòng t
n n
11
n
.
YES NO
INPUT OUTPUT
2 NO
3 YES
911
97625999
91125426
5
113
12340
123440
12345
98346
12
y
[1]. http://diendan.congdongcviet.com/threads/t329887::cau-truc-du-lieu-
trie-cay-tien-to.cpp
[2].https://vi.wikipedia.org/wiki/Trie
[3]. http://vn.spoj.com
13
tv ................................ ................................ ................................ ................................ ..... 2
II. N i dung ................................ ................................ ................................ ................................ ...... 2
1.Trie là gì? ................................ ................................ ................................ ................................ .. 2
2. L i th c a trie so v i các c u trúc d li u tìm ki m khác ................................ .................... 3
3. ng d ng c a ki u d li u Cây ti n t ................................ ................................ .................. 3
4. Các thao n v i ki u d li u trie ................................ ................................ ................ 3
4.1 khai báo ................................ ................................ ................................ ................................ .. 3
5. Các bài t p minh h a ................................ ................................ ................................ ............... 6
................................ ................................ ................................ ................................ .... 13
................................ ................................ ................................ ................... 13
14