-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ14425.java
76 lines (54 loc) · 1.71 KB
/
BOJ14425.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.*;
public class Main {
public static final int ALPHABET_SIZE = 26;
static class Trie {
Trie[] nxt;
boolean isFinished;
public Trie() {
this.nxt = new Trie[ALPHABET_SIZE];
this.isFinished = false;
}
public void insert(String str, int idx) {
if (idx == str.length()) {
isFinished = true;
return;
}
int index = str.charAt(idx) - 'a';
if (this.nxt[index] == null) {
this.nxt[index] = new Trie();
}
this.nxt[index].insert(str, idx + 1);
}
public boolean search(String str, int idx) {
if (idx == str.length()) {
return isFinished;
}
int index = str.charAt(idx) - 'a';
if (this.nxt[index] != null) {
return this.nxt[index].search(str, idx + 1);
} else {
return false;
}
}
}
public static int N, M;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] nmInfo = br.readLine().split(" ");
N = Integer.parseInt(nmInfo[0]);
M = Integer.parseInt(nmInfo[1]);
Trie tree = new Trie();
for (int i = 0; i < N; ++i) {
String str = br.readLine();
tree.insert(str, 0);
}
int cnt = 0;
for (int i = 0; i < M; ++i) {
String queryString = br.readLine();
if (tree.search(queryString, 0)) {
cnt++;
}
}
System.out.println(cnt);
}
}