Algorithm/boj

백준 9046 복호화 //map을 정렬하는방법, vector로 옮겨라, 문자열은 map필요X 배열만으로됨

Mini_96 2023. 7. 10. 03:14

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

 

9046번: 복호화

입력의 T(1 ≤ T ≤ 20)는 테스트 케이스로, 입력 제일 상단에 주어진다. 각각의 테스트 케이스는 한 줄마다 소문자와 공백으로 이루어진 영어 문장이 주어진다. 이 문장의 길이는 적어도 1이상이

www.acmicpc.net

* map을 정렬하는방법, vector로 옮겨라

vector<pair<int,int>> v(m.begin(), m.end());

이후 sort 하면된다

sort(v.begin(), v.end(),cmp)

 

* 문자 index는 map필요X 배열만으로됨

사실 이문제는 문자를 index로 쓰기때문에 a[문자-'a']++ 을 카운트로 사용하는것 만으로 해결된다.

map을 쓸필요가 없었다..

 

- 숫자 to int

[문자-'0']

 

- 알파벳 to int

[문자-'a'] // -a임에 주의하자

 

#include <bits/stdc++.h>

using namespace std;

int n;
string s;
map<char, int> m;

int main() {
	cin >> n;
	n++;
	while (n--) {
		m.clear();
		getline(cin, s);

		for (int i = 0; i < s.size(); ++i) {
			if (s[i] == ' ') continue;

			m[s[i]]++;
		}

		/*for (auto i : m) {
			cout << i.first <<" : " << i.second << endl;
		}*/

		vector<pair<int,int>> v(m.begin(), m.end());
		//for (pair<int, int> it : v) cout << it.first << ":" << it.second << " ";
		//cout << "\n==============================\n";

		sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b) {
			return a.second > b.second;
		});
		//sort(m.begin(), m.end());

		if (v.size() >= 2 && v[0].second == v[1].second)
			cout << "?" << "\n";
		else if(v.size())
			cout << (char)v[0].first << "\n";
	}
}