Skip to content

Commit

Permalink
feat(string/trie.md): Add Java code (OI-wiki#6044)
Browse files Browse the repository at this point in the history
* feat(string/trie.md): Add Java code

* style: format markdown files with remark-lint

* reformat

* style: format markdown files with remark-lint

* reformat

* style: format markdown files with remark-lint

* reformat

* update struct

---------

Co-authored-by: 24OI-bot <15963390+24OI-bot@users.noreply.github.com>
  • Loading branch information
aofall and 24OI-bot authored Dec 11, 2024
1 parent 823f1cf commit 499f627
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/string/trie.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,42 @@ trie 的结构非常好懂,我们用 $\delta(u,c)$ 表示结点 $u$ 的 $c$
return self.exist[p]
```

=== "Java"
```java
public class Trie {

int[][] tree = new int[10000][26];
int cnt = 0;
boolean[] end = new boolean[10000];

public void insert(String word) {
int p = 0;
char[] chars = word.toCharArray();
for (int i = 0; i < chars.length; i++) {
int c = chars[i] - 'a';
if (tree[p][c] == 0) {
tree[p][c] = ++cnt;
}
p = tree[p][c];
}
end[p] = true;
}

public boolean find(String word) {
int p = 0;
char[] chars = word.toCharArray();
for (int i = 0; i < chars.length; i++) {
int c = chars[i] - 'a';
if (tree[p][c] == 0) {
return false;
}
p = tree[p][c];
}
return end[p];
}
}
```

## 应用

### 检索字符串
Expand Down

0 comments on commit 499f627

Please sign in to comment.