class Solution{public: struct trie { bool is_exist = false; bool is_end = false; vector<trie> childs; }; trie *root; void make_tree(trie *current_t, string &s, int current_idx) { if (current_t->childs.size() == 0) { current_t->childs = vector<trie>(27); } char ch = s[current_idx]; trie *next_t = &current_t->childs[ch - 'a']; next_t->is_exist = true; if (current_idx == s.size() - 1) { next_t->is_end = true; return; } return make_tree(next_t, s, current_idx + 1); } bool is_concatenated(trie *current_t, trie *check_t, string &s, int current_idx, int count) { if (s.size() == current_idx) { return 0 < count && check_t->is_end; } char ch = s[current_idx]; trie *next_t = &current_t->childs[ch - 'a']; if (check_t->childs.size() == 0) { return is_concatenated(current_t, root, s, current_idx, count + 1); } trie *next_check_t = &check_t->childs[ch - 'a']; bool ret = false; if (next_check_t->is_exist) { if(next_check_t->is_end) { ret |= is_concatenated(next_t, root, s, current_idx+1, count + 1); } if(ret) { return ret; } ret |= is_concatenated(next_t, next_check_t, s, current_idx+1, count); } return ret; } vector<string> findAllConcatenatedWordsInADict(vector<string> &words) { vector<string> ret; trie tree; tree.is_exist = true; root = &tree; for (auto &s : words) { make_tree(&tree, s, 0); } for (auto &s : words) { if (is_concatenated(&tree, &tree, s, 0, 0)) { ret.push_back(s); } } return ret; }};if 덕지덕지..처음엔 정렬하고 트리만들면서 체크하려고 했는데그냥 트리만들고 체크함
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.