#include <string> #include <vector> #include <algorithm> #include <iostream> using namespace std; vector<vector<string>> sub_key; vector<vector<string>> store; vector<string> str_tokenization(string str) { vector<string> store; string temp = ""; int index = 0; while(index < str.length()) { temp = temp + str[index++]; if (str[index] == ',') { store.push_back(temp); temp = ""; index++; } } return store; } bool compare(vector<string> a, vector<string> b) { vector<string> token_a = str_tokenization(a[0]); vector<string> token_b = str_tokenization(b[0]); if (token_a.size() != token_b.size()) { return token_a.size() < token_b.size(); } return a < b; } void DFS(int count, vector<string> str) { if (count >= sub_key[0].size()) { return; } DFS(count + 1, str); for (int i = 0; i < str.size(); i++) { str[i] = str[i] + sub_key[i][count] + ","; } store.push_back(str); DFS(count + 1, str); } int solution(vector<vector<string>> relation) { int answer = 0; for (int i = 0; i < relation.size(); i++) { sub_key.push_back(relation[i]); } vector<string> str(sub_key.size(), ""); DFS(0, str); sort(store.begin(), store.end(), compare); int check = 0, count = 0; vector<vector<string>> tokens_a; vector<vector<string>> tokens_b; for (int index = 0; index < store.size(); index++) { bool flag = false; for (int i = 0; i < store[index].size(); i++) { for (int j = 0; j < store[index].size(); j++) { if (store[index][i] == store[index][j] && i != j) { flag = true; i = j = str.size(); } } } if (flag) { continue; } for (int index_2 = store.size() - 1; index_2 >= index; index_2--) { count = 0; tokens_a.clear(); tokens_b.clear(); for (int i = 0; i < store[index].size(); i++) { tokens_a.push_back(str_tokenization(store[index][i])); } for (int j = 0; j < store[index_2].size(); j++) { tokens_b.push_back(str_tokenization(store[index_2][j])); } // cout << tokens_a[0].size() << tokens_b[0].size() << "\n"; for (int k = 0; k < tokens_a[0].size(); k++) { for (int h = 0; h < tokens_b[0].size(); h++) { for (int u = 0; u < sub_key.size(); u++) { if (tokens_a[u][k] != tokens_b[u][h]) { break; } if (u == sub_key.size() - 1) { count++; } } } } if (count >= tokens_a[0].size()) { store.erase(store.begin() + index_2); } } index--; answer++; } // answer = store.size(); return answer; } | cs |
#include <string> #include <vector> #include <algorithm> #include <map> using namespace std; vector<string> store; bool compare(string a, string b) { return a.size() < b.size(); } void DFS(int limit, int count, string str) { if (count >= limit) { return; } DFS(limit, count + 1, str); str = str + to_string(count); store.push_back(str); DFS(limit, count + 1, str); } int solution(vector<vector<string>> relation) { int answer = 0; string temp; DFS(relation[0].size(), 0, temp); sort(store.begin(), store.end(), compare); string str; map<string, bool> M; for (int index = 0; index < store.size(); index++) { bool flag = false; M.clear(); for (int i = 0; i < relation.size(); i++) { str = ""; for (int j = 0; j < store[index].size(); j++) { str = str + relation[i][store[index][j] - '0'] + ","; } if(M[str]) { flag = true; break; } else { M[str] = true; } } if (flag) { continue; } for (int index_2 = store.size() - 1; index_2 >= index; index_2--) { int count = 0; for (int k = 0; k < store[index].size(); k++) { for (int h = 0; h < store[index_2].size(); h++) { if (store[index][k] == store[index_2][h]) { count++; break; } } } if (count >= store[index].size()) { store.erase(store.begin() + index_2); } } index--; answer++; } return answer; } | cs |
'problem solving' 카테고리의 다른 글
[프로그래머스]자물쇠와 열쇠 (0) | 2020.08.30 |
---|---|
[프로그래머스]수식 최대화 (0) | 2020.08.27 |
[BOJ]13460번: 구슬 탈출2 (0) | 2020.07.19 |
[BOJ]15683번: 감시 (0) | 2020.07.18 |
[프로그래머스]종이접기 (0) | 2020.07.17 |