일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- pm2
- MongoDB
- JavaScript
- PROJECT
- 백준 14725번
- 자바스크립트
- 백준 16565번
- Github
- 게임 서버 아키텍처
- ERD
- Next
- 그래프 탐색
- 그리디
- html5
- trie
- branch
- insomnia
- HTTP
- 이분 탐색
- string
- map
- localstorage
- MySQL
- 백준 5670번
- ccw 알고리즘
- Prisma
- router
- 트라이
- Express.js
- 백준 13334번
Archives
- Today
- Total
dh_0e
[PS] 개미굴 / C++ (백준 14725번) 본문
자료구조 Trie를 변형해서 풀 수 있는 문제
- map< string, Trie* >로 next에 key값으로 string, value에 Trie의 포인터 값을 저장하게끔 map을 만들어 준 후, input 데이터를 저장해준다.
- dfs형식의 print 함수를 적절히 만들어 next에 저장된 tree 형식의 데이터를 출력
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
struct Trie {
bool finish;
map< string, Trie* > next;
Trie() :finish(false) {};
~Trie() {
for (auto it = next.begin(); it != next.end(); it++)
delete(it->second);
};
void insert(vector<string> vec, int cur, int last) {
string key = vec[cur];
if (next.find(key) == next.end()) {
next.insert({ key, new Trie() });
}
if (cur == last - 1) {
finish = true;
return;
}
next[key]->insert(vec, cur + 1, last);
return;
}
void print(int cur) {
for (auto it = next.begin(); it != next.end(); it++) {
for(int i=0; i<cur; i++)printf("--");
cout << (*it).first << endl;
((*it).second)->print(cur + 1);
}
}
};
int main()
{
int n;
Trie* tree = new Trie();
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int k;
scanf("%d", &k);
vector<string> vec;
for (int j = 0; j < k; j++) {
string str;
cin >> str;
vec.push_back(str);
}
tree->insert(vec, 0, k);
}
tree->print(0);
return 0;
}
'알고리즘 > Baekjoon' 카테고리의 다른 글
[PS] N포커 / C++ (백준 16565번) (0) | 2025.03.10 |
---|---|
[PS] 휴대폰 자판/ C++ (백준 5670번) (0) | 2025.03.10 |
[PS] 전화번호 목록 / C++ (백준 5052번) (0) | 2025.03.05 |
[PS] 열쇠 / C++ (백준 9328번) (0) | 2025.02.13 |
[PS] 텀 프로젝트 / C++ (백준 9466번) (0) | 2025.02.04 |