Created
June 13, 2023 13:42
-
-
Save dkwktm45/415661f6069524b29481e5c69e8f9d34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Scanner; | |
import java.util.function.Function; | |
class Node { | |
HashMap<Character, Node> child; | |
public Node() { | |
this.child = new HashMap<>(); | |
} | |
} | |
class Trie { | |
Node root; | |
public Trie() { | |
this.root = new Node(); | |
} | |
public void insert(String str) { | |
Node cur = this.root; | |
for (int i = 0; i < str.length(); i++) { | |
char c = str.charAt(i); | |
cur.child.putIfAbsent(c,new Node()); | |
cur = cur.child.get(c); | |
} | |
} | |
public boolean search(String str){ | |
Node cur = this.root; | |
for (int i = 0; i < str.length(); i++) { | |
char c = str.charAt(i); | |
if (cur.child.containsKey(c)) { | |
cur = cur.child.get(c); | |
}else{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
Trie trie = new Trie(); | |
String[] nList = sc.nextLine().split(" "); | |
int n = Integer.parseInt(nList[0]); | |
int k = Integer.parseInt(nList[1]); | |
for (int i = 0; i < n; i++) { | |
trie.insert(sc.next()); | |
} | |
int cnt = 0; | |
for (int i = 0; i < k; i++) { | |
if(trie.search(sc.next())){ | |
cnt++; | |
} | |
} | |
System.out.println(cnt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment