๋ฌธ์
์ฝ๋
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map<string,int>cnt;
map<int,int>len_max;
vector<string>combi_arr;
void combination(int index, string combi, string origin, int max_len){
for(int i=index; i<max_len; i++){
string inp = combi+origin[i];
combi_arr.push_back(inp);
combination(i+1, inp, origin, max_len);
}
}
vector<string> solution(vector<string> orders, vector<int> course) {
vector<string> answer;
for(string order:orders){
set<string>ncr;
for(int i=0; i<order.length(); i++) combination(i, "", order, order.length());
for(string &combi:combi_arr) sort(combi.begin(),combi.end());
for(string combi:combi_arr) ncr.insert(combi);
for(auto it=ncr.begin(); it!=ncr.end(); it++) cnt[*it]++;
combi_arr.clear();
}
for(auto it = cnt.begin(); it!=cnt.end(); it++){
int len = (it->first).length();
if(len_max[len]<it->second) len_max[len] = it->second;
}
for(auto it = cnt.begin(); it!=cnt.end(); it++){
for(int cos:course){
if(it->second>=2&&(it->first).length()==cos&&it->second==len_max[(it->first).length()]){
answer.push_back(it->first);
}
}
}
sort(answer.begin(),answer.end());
return answer;
}
ํ์ด(45๋ถ ์ด์)
์ฒ์์ ์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ์๋๋ฐ ์๊ฐ ์ด๊ณผ ๋๋ฌธ์ 50์ ์ด ๋ ์ ๊ต์ฅํ ๊ณ ๋ฏผ์ ๋ง์ด ํ ๋ฌธ์ ์ ๋๋ค.
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map<string,int>cnt;
map<int,int>len_max;
vector<string> solution(vector<string> orders, vector<int> course) {
vector<string> answer;
for(string &order:orders) sort(order.begin(),order.end()); //๋ฏธ๋ฆฌ ์ ๋ ฌ
for(string order:orders){
map<string,int>visited;
set<string>ncr;
do{
string ord = order;
while(!ord.empty()){
ncr.insert(ord);
ord.pop_back();
}
}while(next_permutation(order.begin(),order.end()));
for(auto it=ncr.begin(); it!=ncr.end(); it++){
string inp = *it;
sort(inp.begin(),inp.end());
if(!visited[inp]){
cnt[inp]++;
visited[inp]=1;
}
}
}
for(auto it = cnt.begin(); it!=cnt.end(); it++){
int len = (it->first).length();
if(len_max[len]<it->second) len_max[len] = it->second;
}
for(auto it = cnt.begin(); it!=cnt.end(); it++){
for(int cos:course){
if(it->second>=2&&(it->first).length()==cos&&it->second==len_max[(it->first).length()]){
answer.push_back(it->first);
}
}
}
sort(answer.begin(),answer.end());
return answer;
}
์๋๋ง๋ค ์ฃผ๋ฌธํ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํด์ผ ํ๋๋ฐ ์ข ํธํ๊ฒ ํด ๋ณด์๊ณ next_permutation์ ์ฌ์ฉํ ๊ฒ์ด ์๊ฐ ์ด๊ณผ์ ์์ธ์ด์์ต๋๋ค. ์๊ฐ ์ด๊ณผ ๋จ์๋ง์ ๋ฐ๋ก ๋ฐฑํธ๋ํน์ผ๋ก ๊ตฌํ์ ํ๋ฉด ์ข์์ ํ ๋ฐ ๋ ์ธ๋ฐ์๋ ๊ณ ์ง ๋๋ฌธ์ next_permutation์ ์ฌ์ฉํด์ ์ต์ง๋ก ์ด๋ป๊ฒ๋ ํด๋ณด๋ ค๊ณ ์ด๊ฒ์ ๊ฒ ๊ฑด๋๋ฆฐ ๊ฒ ์๊ฐ๋ญ๋น๊ฐ ๋งค์ฐ ์ปธ์ต๋๋ค..์ํด,.. ๊ณ ์ง์ ์ข ์ค์ฌ์ผ๊ฒ ์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ํ๋ก๊ทธ๋๋จธ์ค(Programmers)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ํ๋ณดํค(Level 2) (0) | 2022.04.17 |
---|---|
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์นดํซ(Level 2) (0) | 2022.04.14 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - 124 ๋๋ผ์ ์ซ์(Level 2) (0) | 2022.04.14 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์์ ์ฐพ๊ธฐ(Level 2) (0) | 2022.04.13 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - [1์ฐจ] ๋น๋ฐ ์ง๋(Level 1) (0) | 2022.04.13 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - 3์ง๋ฒ ๋ค์ง๊ธฐ(Level 1) (0) | 2022.04.13 |
๋๊ธ