문제

 

 

풀이

n + m번 동안 문자열을 받으며 value가 1보다 크면 듣보잡이기 때문에 해당 조건을 만족하는 문자열들을 따로 배열에 담은 후, 배열의 크기를 출력하고, 원소들을 사전순으로 출력하면 된다.

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	cout.tie(NULL);

	int n, m;
	string s;

	map<string, int> mp;
	vector<string> result;

	cin >> n >> m;

	for (int i = 0; i < n + m; i++) {
		cin >> s;

		mp[s]++;
		if (mp[s] > 1)
			result.push_back(s);
	}

	sort(result.begin(), result.end());

	cout << result.size() << "\n";

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

	return 0;
}

'백준 > 집합과 맵' 카테고리의 다른 글

10816번: 숫자 카드 2 [C++]  (0) 2023.03.31
1620번: 나는야 포켓몬 마스터 이다솜 [C++]  (0) 2023.03.31
14425번: 문자열 집합 [C++]  (0) 2023.03.31
10815번: 숫자 카드 [C++]  (0) 2023.03.31

문제

 

 

풀이

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n, num;
	unordered_map<int, int> m;

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> num;

		m[num]++;
	}

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> num;

		cout << m[num] << " ";
	}

	return 0;
}

'백준 > 집합과 맵' 카테고리의 다른 글

1764번: 듣보잡 [C++]  (0) 2023.03.31
1620번: 나는야 포켓몬 마스터 이다솜 [C++]  (0) 2023.03.31
14425번: 문자열 집합 [C++]  (0) 2023.03.31
10815번: 숫자 카드 [C++]  (0) 2023.03.31

문제

아래 링크 참고

 

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

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

www.acmicpc.net

 

 

풀이

<이름, 번호>와 <번호, 이름> 구조의 맵을 따로 만들어서 이름이 들어오면 번호, 번호가 들어오면 이름을 출력하도록 하였다.

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	cout.tie(NULL);

	int n, m;
	int cnt = 1;
	map<string, int> pokemonNum;
	map<int, string> pokemonName;

	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		string s;

		cin >> s;

		pokemonNum[s] = cnt + i;
		pokemonName[cnt + i] = s;
	}

	for (int i = 0; i < m; i++) {
		string s;

		cin >> s;

		if (s[0] >= 'A' && s[0] <= 'Z')
			cout << pokemonNum[s] << "\n";
		else
			cout << pokemonName[stoi(s)] << "\n";
	}

	return 0;
}

'백준 > 집합과 맵' 카테고리의 다른 글

1764번: 듣보잡 [C++]  (0) 2023.03.31
10816번: 숫자 카드 2 [C++]  (0) 2023.03.31
14425번: 문자열 집합 [C++]  (0) 2023.03.31
10815번: 숫자 카드 [C++]  (0) 2023.03.31

문제

 

 

풀이

map 자료구조를 사용해 풀면된다.

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
	int n, m;
	int cnt = 0;
	map<string, bool> mp;

	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		string s;

		cin >> s;

		mp.insert({ s, true });
	}

	for (int i = 0; i < m; i++) {
		string s;

		cin >> s;

		if (mp[s] == true) cnt++;
	}

	cout << cnt;

	return 0;
}

'백준 > 집합과 맵' 카테고리의 다른 글

1764번: 듣보잡 [C++]  (0) 2023.03.31
10816번: 숫자 카드 2 [C++]  (0) 2023.03.31
1620번: 나는야 포켓몬 마스터 이다솜 [C++]  (0) 2023.03.31
10815번: 숫자 카드 [C++]  (0) 2023.03.31

문제

 

 

풀이

시간초과에 주의하자

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n, num;
	unordered_map<int, int> m;

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> num;

		m[num]++;
	}

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> num;

		cout << m[num] << " ";
	}

	return 0;
}

'백준 > 집합과 맵' 카테고리의 다른 글

1764번: 듣보잡 [C++]  (0) 2023.03.31
10816번: 숫자 카드 2 [C++]  (0) 2023.03.31
1620번: 나는야 포켓몬 마스터 이다솜 [C++]  (0) 2023.03.31
14425번: 문자열 집합 [C++]  (0) 2023.03.31

+ Recent posts