Created
September 9, 2020 05:19
BOJ 14725
This file contains hidden or 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
//14725 | |
#include <iostream> | |
#include <string> | |
#include <cstring> | |
#include <cstdio> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
typedef long long ll; | |
struct Node { | |
string val; | |
vector<Node> next; | |
Node() {} | |
Node(string val) : val(val) {} | |
}; | |
vector<Node> tree; | |
int k; | |
//pi : parent의 인덱스 | |
//si : s의 | |
void makeLink(vector<Node>& parent, vector<string> s, int pi, int si) { | |
//종료 | |
if (si == k) { | |
return; | |
} | |
//자식 탐색 | |
for (int i = 0; i < parent.size(); i++) { | |
if (s[si] == parent[i].val) { | |
pi = i; | |
Node child; | |
if (si < s.size() - 1) { | |
child.val = s[si + 1]; | |
//중복체크 | |
bool chk = false; | |
for (int j = 0; j < parent[pi].next.size(); j++) { | |
if (parent[pi].next[j].val == child.val) { | |
chk = true; | |
break; | |
} | |
} | |
if(!chk) parent[pi].next.push_back(child); | |
} | |
break; | |
} | |
} | |
makeLink(parent[pi].next, s, pi, si + 1); | |
} | |
//정렬 | |
bool compare(Node a, Node b) { | |
return a.val < b.val; | |
} | |
void prt(vector<Node> parent, int hier) { | |
//알파벳 순 출력을 위한 정렬 | |
sort(parent.begin(), parent.end(), compare); | |
//recursive로 답 출력 | |
for (int i = 0; i < parent.size(); i++) { | |
for (int j = 0; j < 2 * hier; j++) cout << '-'; | |
cout << parent[i].val << '\n'; | |
prt(parent[i].next, hier + 1); | |
} | |
} | |
int main() { | |
cin.tie(nullptr); | |
cout.tie(NULL); | |
ios_base::sync_with_stdio(false); | |
int t; cin >> t; | |
while (t--) { | |
cin >> k; | |
vector<string> tmp; | |
for(int i=0; i<k; i++) { | |
string value; cin >> value; | |
tmp.push_back(value); | |
} | |
//중복 체크 | |
bool chk = false; | |
for (int i = 0; i < tree.size(); i++) { | |
if (tree[i].val == tmp[0]) { | |
chk = true; | |
break; | |
} | |
} | |
//process | |
if(!chk) tree.push_back({ tmp[0] }); | |
makeLink(tree, tmp, -1, 0); | |
} | |
//answer | |
prt(tree, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment