[백준 20920번 C/C++] 영단어 암기는 괴로워 

 

 

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

 

20920번: 영단어 암기는 괴로워

첫째 줄에는 영어 지문에 나오는 단어의 개수 $N$과 외울 단어의 길이 기준이 되는 $M$이 공백으로 구분되어 주어진다. ($1 \leq N \leq 100\,000$, $1 \leq M \leq 10$) 둘째 줄부터 $N+1$번째 줄까지 외울 단

www.acmicpc.net


 

 

해결전략

 

map을 사용하여 중복하여 같은 문자가 들어가면 int값을 늘리는 방식으로 몇 번 들어가느지 체크한다.

 

문자열의 길이는 length()를 사용하여 체크한다.

 

if (myMap.find(input) != myMap.end())

로 입력값이 있는지 체크한다.

 


 

코드

 

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

bool Comp(const pair<string, int>& a, const pair<string, int>& b)
{
	if (a.second == b.second)
	{
		if (a.first.length() == b.first.length())
			return a.first < b.first;
		return a.first.length() > b.first.length();
	}
	else
		return a.second > b.second;
}

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

	int n, m;
	cin >> n >> m;

	map<string, int> myMap;
	string input;

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

		if(input.length() >= m)
		{
			if (myMap.find(input) != myMap.end())
				myMap[input]++;
			else
				myMap[input] = 1;
		}
	}

	vector<pair<string, int>> vec(myMap.begin(), myMap.end());
	sort(vec.begin(), vec.end(), Comp);

	for (const auto& i : vec)
		cout << i.first << "\n";

	return 0;
}