Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- jdbc
- 보행자 천국
- 튜플
- trie
- 가사 검색
- 프로그래머스
- 티스토리 open api
- Spring Boot
- Tistory
- 트라이 #trie #알고리즘
- 알고리즘
- 징검다리 건너기
- 카카오 인턴
- bulk update
- 호텔 방 배정
- 불량 사용자
- pycon
- Open API
- 티스토리
- 크레인 인형뽑기 게임
- Python
- 트라이
- CleanCode
Archives
- Today
- Total
택시짱의 개발 노트
[백준] 14425번 문자열 집합 (트라이) 본문
링크
https://www.acmicpc.net/problem/14425
14425번: 문자열 집합
첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다. 다음 N개의 줄에는 집합 S에 포함되어 있는 문자열들이 주어진다. 다음 M개의 줄에는 검사해야 하는 문자열들이 주어진다. 입력으로 주어지는 문자열은 알파벳 소문자로만 이루어져 있으며, 길이는 500을 넘지 않는다. 집합 S에 같은 문자열이 여러 번 주어지는 경우는 없다.
www.acmicpc.net
풀이
M개의 문자열 중에서 집합 S에 포함되어 있는 것이 총 몇 개인지 구하는 문제이다.
간단하게 집합 S를 map에 추가 하고나서
M개의 문자열을 map과 매핑하여 포함 여부를 확인하면 된다.
1. map 이용
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<set>
#include<map>
#include<cstring>
#include<functional>
#include<cmath>
#include<stack>
#define SIZE 30
const int INF = 2000000000;
using namespace std;
typedef long long int ll;
set<string> _set;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int N, M; cin >> N >> M;
for (int i = 0; i < N; i++) {
string word; cin >> word;
_set.insert(word);
}
int cnt = 0;
for (int i = 0; i < M; i++) {
string word; cin >> word;
if (_set.find(word) != _set.end())
cnt++;
}
cout << cnt;
}
또 다른 방법으로는 trie를 이용하여 풀었습니다.
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<set>
#include<map>
#include<cstring>
#include<functional>
#include<cmath>
#include<stack>
#define SIZE 30
const int INF = 2000000000;
using namespace std;
typedef long long int ll;
struct Trie {
Trie *node[SIZE];
bool is_finish, is_child;
Trie() {
fill(node, node + SIZE, nullptr);
is_finish = is_child = false;
}
~Trie() {
for (int i = 0; i < SIZE; i++) {
if (node[i])
delete node[i];
}
}
void insert(string &word, int word_size, int index) {
if (index >= word_size) {
is_finish = true;
return;
}
int word_index = word.at(index) - 'a';
if (node[word_index] == NULL) {
node[word_index] = new Trie();
is_child = true;
}
node[word_index]->insert(word, word_size, index + 1);
}
bool find(string &word, int word_size, int index) {
bool res = false;
if (index >= word_size) {
if (is_finish)
return true;
else
return false;
}
else if (index < word_size) {
int word_index = word.at(index) - 'a';
if (node[word_index] == NULL || is_finish == true || is_child == false) {
return false;
}
res = node[word_index]->find(word, word_size, index + 1);
}
return res;
}
};
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
Trie root;
int N, M; cin >> N >> M;
for (int i = 0; i < N; i++) {
string word; cin >> word;
root.insert(word, word.size(), 0);
}
int cnt = 0;
for (int i = 0; i < M; i++) {
string word; cin >> word;
if (root.find(word, word.size(), 0)) {
cnt++;
}
}
cout << cnt;
}
그 결과
위의 결과가 trie를 이용하여 푼 결과이고
밑의 결과가 map를 이용하여 푼 결과이다.
일단 map으로 푼 문제의 시간복잡도는 o(문자열의 갯수(10000)*map의 탐색 시간(log10000)) 이고
trie를 이용한 문제의 시간복잡도는 o(문자열의 갯수(10000)* trie의 탐색 시간 즉 문자열의 길이(500)) 이므로
trie을 이용한것이 map을 이용한것보다 시간이 올래 걸리는것을 알 수 있습니다.
반응형
'알고리즘' 카테고리의 다른 글
[백준] 5670번 휴대폰 자판 (0) | 2020.03.24 |
---|---|
[백준] 14225번 부분수열의 합 (비트마스크) (0) | 2020.03.24 |
[프로그래머스] 단어 변환 (0) | 2020.03.20 |
[백준] 17822번 원판 돌리기 (0) | 2020.03.20 |
[프로그래머스] 줄 서는 방법 (0) | 2020.03.19 |
Comments