Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 게리멘더링2
- 5397
- 버킷 정렬
- 백준 17779
- 자료구조
- qorwns
- 별 찍기 10
- C/C++ 구현
- 해시구현
- 1764
- 원판 돌리기
- 조세퍼스 순열
- 구현
- 백준 17822
- 백준 1406
- 백준 2447
- 백준 17471
- 풀이
- ㅣ풀이
- c#
- 백준
- 스택의 특징
- AVL 시간 복잡도
- heap
- 시간 복잡도
- 해시 구현
- 백준 5397
- 백준 1158
- dfs
- Stack 이란
Archives
- Today
- Total
홍시홍의 프로그래밍
[2020 카카오 공채] 가사 검색 본문
분류
시뮬레이션
요구사항
가사에 사용된 모든 단어들이 담긴 배열 words와 찾고자 하는 키워드가 담긴 배열 queries가 주어질 때, 각 키워드 별로 매치된 단어가 몇 개인지 순서대로 배열에 담아 반환하도록 solution 함수를 완성
풀이
fro?? 라면 앞의 3단어만 fro라면 모든 단어가 해당 단어로 count 된다
??fro 라면 뒤의 3단어만 fro라면 모든 단어가 해당 단어로 count 된다
그러므로 길이와 앞 혹은 뒤 단어만 비교하면 된다
앞 혹은 뒤의 index를 반환하는 함수를 만들어 주어진 배열과 길이와 해당 index의 문자를 비교해본다
bool com(string a, string b){
if(a.size() < b.size()) return true;
return false;
}
int Getfindstart(string str){
int flag=0;
for(int i =0 ; i <str.size() ; i++){
char now_ch = str[i];
if(now_ch!='?') return i;
}
}
int Getfindend(string str){
int flag=0;
for(int i =str.size()-1 ; i >=0 ; i--){
char now_ch = str[i];
if(now_ch !='?') return i;
}
}
vector<int> solution(vector<string> words, vector<string> queries) {
vector<int> answer;
sort(words.begin(),words.end(),com);
vector<string> my_words[10100];
for(auto i : words){
my_words[i.size()].push_back(i);
}
for(int i=0 ; i <queries.size() ; i++){
string now_str= queries[i];
int start_pos = Getfindstart(queries[i]);
int end_pos = Getfindend(queries[i]);
string temp_str= now_str.substr(start_pos,end_pos-start_pos+1);
int now_size= now_str.size();
int cnt=0;
for(int j=0 ; j <my_words[now_size].size() ; j++){
if(temp_str==my_words[now_size][j].substr(start_pos,end_pos-start_pos+1)){
cnt++;
}
}
answer.push_back(cnt);
}
return answer;
}
'알고리즘 문제풀이 > 카카오' 카테고리의 다른 글
[2020 카카오 공채] 괄호 변환 (0) | 2020.08.08 |
---|---|
[2020 카카오 공채] 문자열 압축 (0) | 2020.08.05 |
[2017 카카오 코딩테스트] 셔틀 버스 (0) | 2020.08.04 |
[2017 카카오 코딩테스트] 프렌즈4블록 (0) | 2020.08.04 |
[2017 카카오 코딩테스트] 뉴스 클러스터링 (0) | 2020.08.04 |
Comments