알고리즘 분류에 이분 탐색이라고 적혀있었고, 문제만 읽어봐도 숫자로 된 것은 직접적으로 찾아들어가 반환해주면 된다. 문제는 string으로 된 포켓몬 이름들을 찾아 들어가는 것이 문제였다. 정답 비율 보고 이건 순차적으로 찾으면 바로 퇴짜겠구나도 감이 올 것이고, 이분 탐색으로 푸는 것이 정당했다. 어찌 되었든 간에 이분 탐색이다!!!!!!!! 참고로 다른 사람들은 Map으로 푸는 것을 선호하는 것 같았다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(pair<int, string> a, pair<int, string> b){
return a.second < b.second;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
vector<pair<int, string>> pocketmonName;
cin >> n >> m;
string numbers[n];
string temp;
for(int i=1; i<=n; i++){
cin >> temp;
pocketmonName.push_back(make_pair(i, temp));
numbers[i-1] = temp;
}
sort(pocketmonName.begin(), pocketmonName.end(), compare); // 포켓몬 이름으로 정렬
for(int i=0; i<m; i++){
cin >> temp;
if(temp[0] >= '0' && temp[0] <= '9'){
int t = stoi(temp);
cout << numbers[t-1] << "\n";
} else{
int start = 0;
int last = n-1;
int mid;
while(start <= last){
mid = (start+last)/2;
if(temp == pocketmonName[mid].second){
cout << pocketmonName[mid].first << "\n";
break;
} else if(temp > pocketmonName[mid].second){
start = mid+1;
} else{
last = mid-1;
}
}
}
}
}
벡터에 int, string 순으로 하고 compare 함수를 만들어 string 기준으로 정렬을 했다. 느렸다. 248ms가 나온 코드가 이거다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m;
vector<pair<string, int>> pocketmonName;
int inputString(string &temp){
int start = 0;
int last = n-1;
int mid;
while(start <= last){
mid = (start+last)/2;
if(temp == pocketmonName[mid].first){
return pocketmonName[mid].second;
} else if(temp > pocketmonName[mid].first){
start = mid+1;
} else{
last = mid-1;
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
string numbers[n];
string temp;
for(int i=1; i<=n; i++){
cin >> temp;
pocketmonName.push_back(make_pair(temp, i));
numbers[i-1] = temp;
}
sort(pocketmonName.begin(), pocketmonName.end()); // 포켓몬 이름으로 정렬
for(int i=0; i<m; i++){
cin >> temp;
if(temp[0] >= '0' && temp[0] <= '9'){
int t = stoi(temp);
cout << numbers[t-1] << "\n";
} else{
cout << inputString(temp) << "\n";
}
}
}
벡터의 int , string 순서를 바꿔서 정렬했다. 속도는 128ms로 나왔다. 이 문제를 풀면서 신기했던 것은 문자를 비교할 때
이런 식으로 값이 크고 다른 것을 알 수 있다.< 문자열을 비교할 때 "사전에 먼저 나오면 작다" 그래서 elseif(temp > pocketmonName[mid].first) 이런 식으로 비교가 가능하다. 또 하나 신기했던 것은 숫자와 문자를 받을 때 확인 방법이다. 숫자는 앞자리가 숫자면 숫자라
if(temp[0] >= '0' && temp[0] <= '9') 이런 식으로 확인하면 된다. 그리고 stoi로 문자 자체를 int로 바꿔 출력하면 된다.
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
class Solution { public int solution(int[] priorities, int location) {
int answer = 0;
Queue<print_info> prior_Q = new LinkedList<print_info>();
Iterator<print_info> it;
// Queue 생성
for (int i = 0; i<priorities.length; i++)
if(i != location)
prior_Q.add(new print_info(priorities[i], false));
else
prior_Q.add(new print_info(priorities[i], true));
print_info A;
boolean check = true;
while(!prior_Q.isEmpty()) {
check = true;
A = prior_Q.poll();
it = prior_Q.iterator();
while (it.hasNext()) {
if (A.data < it.next().data) {
check = false;
break;
}
}
// check가 true라면 큐 안 요소 중에서 A가 크다는 것을 의미함
if (check && A.flag){
answer++;
break;
} else if(check) {
answer++;
} else {
prior_Q.add(new print_info(A.data, A.flag));
}
}
return answer;
}
static class print_info {
int data;
boolean flag;
public print_info(int data, boolean flag){
this.data = data;
this.flag = flag;
}
}
}
c++은 2020.4.24에 짠 코드이고, Java는 대략 2월 초중에 짠 코드이다. c++의 경우 두개의 큐, queue와 우선순위 queue를 사용하여 짠 코드이다. 방금 짜서 그런지 c++이 더 간결하다고 생각이 든다. Java로 짠 2월달 아기는 다시 읽을 때 뭐지? 이 생각을 조금 했다. ㅋㅋㅋㅋ 다시 보니 c++과 동일하게 data와 flag를 가져 내가 원하고자하는 수를 찾고자했다. 다른 점은 우선 순위 큐를 사용하지 않고 이걸 queue를 한 바퀴 돌아서 값이 제일 큰지 확인한다. 그 다음 제일 크고 flag가 true이라면 answer값을 반환한다.
사실 c++로 짜면서도 큐를 한 바퀴 돌까 생각했는데 도무지 귀찮아서 그럴 생각이 들지 않았는데 2월달에 난 귀찮아도 일단 풀었다. 대단하다 ! 굿굿