-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB14426.java
More file actions
56 lines (41 loc) · 1.31 KB
/
B14426.java
File metadata and controls
56 lines (41 loc) · 1.31 KB
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
package Practice;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B14426 { //Á¢µÎ»ç ã±â
static int count;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
Trie trie = new Trie();
for(int i = 0; i < N; i++) trie.insert(br.readLine());
for(int i = 0; i < M; i++) trie.contain(br.readLine());
System.out.println(count);
}
static class Trie {
TrieNode Node = new TrieNode();
void insert(String str) {
TrieNode node = this.Node;
for(int i = 0; i < str.length(); i++) {
int alpha = str.charAt(i) - 'a';
if(node.childNode[alpha] == null) node.childNode[alpha] = new TrieNode();
node = node.childNode[alpha];
}
}
void contain(String str) {
TrieNode node = this.Node;
for(int i = 0; i < str.length(); i++) {
int alpha = str.charAt(i) - 'a';
if(node.childNode[alpha] == null) return;
node = node.childNode[alpha];
}
count++;
}
}
static class TrieNode {
TrieNode[] childNode = new TrieNode[26];
}
}