-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrie.cs
60 lines (54 loc) · 1.36 KB
/
trie.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace N_Puzzle_Game
{
class trie
{
class Node
{
public Node[] child;
public int count;
}
Node root = new Node();
int size = 0;
public trie(int mx)
{
size = mx;
root.count = 0;
root.child = new Node[size];
}
public void add(string state)
{
Node tmp = root;
int idx = 0, x;
while (idx < state.Length)
{
x = state[idx] - 48;
if (tmp.child[x] == null)
{
tmp.child[x] = new Node();
tmp.child[x].child = new Node[size];
tmp.child[x].count = 0;
}
tmp.child[x].count += 1;
idx += 1;
tmp = tmp.child[x];
}
}
public bool is_visited(string state)
{
Node tmp = root;
for (int i = 0; i < state.Length; i++)
{
int x = state[i] - 48;
if (tmp.child[x] != null)
tmp = tmp.child[x];
else return false;
}
return tmp.count > 0;
}
}
}