출처 : https://www.acmicpc.net/problem/14425
14425번: 문자열 집합
첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다. 다음 N개의 줄에는 집합 S에 포함되어 있는 문자열들이 주어진다. 다음 M개의 줄에는 검사해야 하는 문자열들이 주어
www.acmicpc.net
고려사항
- 트라이 자료구조 활용
#include <cstdio>
using namespace std;
struct trie{
bool term;
trie *next[26];
trie(){
term = false;
for(int i=0;i<26;++i){
next[i] = NULL;
}
}
~trie(){
for(int i=0;i<26;++i){
delete next[i];
}
}
void insert(char *str){
if(*str == '\0'){
term = true;
}else{
int tmp = *str - 'a';
if(!next[tmp]){
next[tmp] = new trie();
}
next[tmp]->insert(str+1);
}
}
bool find(char *str){
if(*str == '\0'){
if(term){
return true;
}else{
return false;
}
}else{
int tmp = *str - 'a';
if(!next[tmp]){
return false;
}else{
return next[tmp]->find(str+1);
}
}
}
};
int main() {
trie *root = new trie();
int n, m, ans = 0;
char str[501];
scanf("%d %d", &n, &m);
for(int i=0;i<n;++i){
scanf("%s", str);
root->insert(str);
}
for(int i=0;i<m;++i){
scanf("%s", str);
if(root->find(str)){
++ans;
}
}
printf("%d", ans);
return 0;
}
'BOJ 백준 > 문자열' 카테고리의 다른 글
백준 09935 - 문자열 폭발 (0) | 2020.11.12 |
---|---|
백준 05670 - 휴대폰 자판 (0) | 2020.11.07 |
백준 10988 - 팰린드롬인지 확인하기 (0) | 2020.01.07 |
백준 10545 - 뚜기뚜기메뚜기 (0) | 2020.01.04 |