문제

https://www.acmicpc.net/problem/1620

 

1620번: 나는야 포켓몬 마스터 이다솜

첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면

www.acmicpc.net

풀이

  • unordered_map은 해쉬 테이블로 구현한 자료구조로 탐색 시간 복잡도는 O(1)입니다.
  • map은 Binary Search Tree로 탐색 시간 복잡도는 O(log n)입니다.

저는 2개의 unordered_map을 선언하여 string,int를 구분지어서 만들었습니다.

조심해야 할 점은 count() 선언을 까먹지 말고 써주지 않고 직접 배열을 찾는 방식 (num [i]) 은 그 자체로 노드가 선언되므로 count()를 사용합시다!

코드

#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<string>
using namespace std;
int N, M;
unordered_map<string, int> st;
unordered_map<int,string> num;
int main() {
	cin >> N >> M;

	for (int i = 1; i <= N; i++) {
		string a;
		cin >> a;
		st.insert({ a,i });
		num.insert({ i,a });

	}
	vector<string>result;
	//M입력찾기
	for (int j = 0; j < M; j++) {
		
		string arr;
		cin >> arr;
		
		if (arr[0] < 'A') { //숫자라면
			if (num.count(stoi(arr))>0) {
				result.push_back(num[stoi(arr)]);
			}
		}
		else { //영어 
			if (st.count(arr) > 0) {
				result.push_back(to_string(st[arr]));
			}
		}
		

	}

	for (int i = 0; i < result.size(); i++) {
		cout << result[i] << '\n';
	}

	return 0;
}

'알고리즘 공부 > 백준' 카테고리의 다른 글

[백준] 상어 초등학교 (21608/C++)  (0) 2022.03.12
[백준] 통계학 (2108/C++)  (0) 2022.02.02
[백준] 경로 찾기 (11403/C++)  (0) 2022.01.30
[백준] 최대 힙 (11279/C++)  (0) 2022.01.26
[백준] 최소 힙 (1927/C++)  (0) 2022.01.26

문제

https://www.acmicpc.net/problem/11403

 

11403번: 경로 찾기

가중치 없는 방향 그래프 G가 주어졌을 때, 모든 정점 (i, j)에 대해서, i에서 j로 가는 경로가 있는지 없는지 구하는 프로그램을 작성하시오.

www.acmicpc.net

 

풀이

기본 그래프 문제처럼 풀었다. 알고리즘 분류 및 타 블로그에는 플로이드 와샬로 많이 푼 것 같다. 

하지만 기본 DFS 그래프 탐색으로도 풀 수 있을 것 같아서 그렇게 풀었다. 이런 그래프 문제는 2차원 배열 형식으로 풀기보단 Vector를 사용하는 것이 생각하기 좀 더 수월한 것 같다. 

visited배열을 활용하여 기본 Cycle방지 + 인덱스에 값을 할당하였다.

 

코드

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
int N;
vector<int> map[101];
int visited[101];
void dfs(int k) {

	for (int i = 0; i < map[k].size(); i++) {
		if (visited[map[k][i]] == 0) {
			visited[map[k][i]] = 1; //다음칸
			dfs(map[k][i]);
		}
	}

}
int main() {
	cin >> N;
	
	for (int y = 0; y < N; y++) {
		for (int x = 0; x < N; x++) {
			int a;
			cin >> a;
			if(a==1)map[y].push_back(x);

		}
	}


	for (int i = 0; i < N; i++) {
		memset(visited, 0, sizeof(visited));
		dfs(i);
		
		for (int j = 0; j < N;	j++) {
			cout << visited[j] << " ";
		}
		cout << '\n';
	}

	return 0;
}

문제

https://www.acmicpc.net/problem/11279

 

11279번: 최대 힙

첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가

www.acmicpc.net

풀이

기본 최대 힙 구성 문제였다.

C++의 Priority_queue 를 활용하였다.

 

최소힙 문법 문제는 아래를 참고하자. 

https://trevor522.tistory.com/89

 

[백준] 최소 힙 (1927/C++)

문제 https://www.acmicpc.net/problem/1927 1927번: 최소 힙 첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면..

trevor522.tistory.com

 

 

코드

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
priority_queue<int> pq;
vector<int> result;
int  main() {
	int N;
	cin >> N;

	for (int i = 0; i < N; i++) {
		int a;
		cin >> a;
		if (a == 0) {
			if (pq.empty()) {
				result.push_back(0);
			}
			else {
				result.push_back(pq.top());
				pq.pop();
			}


		}
		else {
			pq.push(a);
		}
		



	}
	for (int j = 0; j < result.size(); j++) {
		cout << result[j] << '\n';

	}



	return 0;
}

문제

https://www.acmicpc.net/problem/1927

 

1927번: 최소 힙

첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0

www.acmicpc.net

풀이

최소힙 기본연습문제이다. 

C++ 의 Priority_queue를 활용하였다. 

최소 힙 같은경우  priority_queue<int,vector<int>,greater<int>> 를 사용하면 된다. 

 

코드

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

priority_queue<int, vector<int>, greater<>> pq;
vector<int> result;
int  main() {
	int N;
	cin >> N;

	for (int i = 0; i < N; i++) {
		int a;
		cin >> a;
		if (a == 0) {
			if (pq.empty()) {
				result.push_back(0);
			}
			else {
				result.push_back(pq.top());
				pq.pop();
			}


		}
		else {
			pq.push(a);
		}
		



	}
	for (int j = 0; j < result.size(); j++) {
		cout << result[j] << '\n';

	}



	return 0;
}

+ Recent posts