백준 14426 접두사 찾기
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int ALPHABET_SIZE = 26;
struct TrieNode{
struct TrieNode *children[ALPHABET_SIZE];
bool isEndOfWord;
};
struct TrieNode *getNode(void){
struct TrieNode *pNode = new TrieNode;
pNode->isEndOfWord = false;
for (int i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
return pNode;
}
void insert(struct TrieNode *root, string key){
struct TrieNode *pCrawl = root;
for (int i = 0; i < key.length(); i++)
{
int index = key[i] - 'a';
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isEndOfWord = true;
}
int search(struct TrieNode *root, string key){
struct TrieNode *pCrawl = root;
for (int i = 0; i < key.length(); i++)
{
int index = key[i] - 'a';
if (!pCrawl->children[index])
return -1;
pCrawl = pCrawl->children[index];
}
return pCrawl->isEndOfWord;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n, m;
cin >> n >> m;
struct TrieNode *root = getNode();
for (int i = 0; i < n; ++i) {
string input;
cin >> input;
insert(root, input);
}
int cnt = 0;
for (int i = 0; i < m; ++i) {
string input;
cin >> input;
if (search(root, input) != -1) ++cnt;
}
cout << cnt;
return 0;
}