문제
Farmer John has been keeping detailed records of his cows as they enter the barn for milking. Each hour, a group of 3 cows enters the barn, and Farmer John writes down their names. For example over a 5-hour period, he might write down the following list, where each row corresponds to a group entering the barn:
BESSIE ELSIE MATILDA FRAN BESSIE INGRID BESSIE ELSIE MATILDA MATILDA INGRID FRAN ELSIE BESSIE MATILDA
Farmer John notes that the same group of cows may appear several times on his list; in the example above, the group of BESSIE, ELSIE, and MATILDA appears three times (even though Farmer John didn't necessarily write their names in the same order every time they entered the barn).
Please help Farmer John count the number of occurrences of the group entering the barn the most.
입력
- Line 1: The number of hours, N, for which Farmer John keeps records (1 <= N <= 1000).
- Lines 2..1+N: Each line contains a list of three space-separated cow names. Each name is between 1 and 10 characters and uses only the letters A-Z.
출력
- Line 1: The number of occurrences of the group entering the barn the most often.
<코드>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int N;
int ans, tmp;
string s[1001][3];
int main()
{
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> s[i][0] >> s[i][1] >> s[i][2];
sort(s[i], s[i] + 3);
}
for (int i = 0; i < N; i++)
{
tmp = 0;
for (int j = 0; j < N; j++)
{
if (s[i][0] == s[j][0] &&
s[i][1] == s[j][1] &&
s[i][2] == s[j][2]) tmp++;
ans = max(ans, tmp);
}
}
cout << ans;
}
풀이 방법
N이 최대 1000이라 O(N^2)의 시간복잡도로 풀었습니다. 각 시간마다 소들의 목록을 받으면 사전순으로 정렬한 후에 목록에 저장하였고 그런다음 brute force방식으로 중복된 멤버목록의 수를 세면서 최대값을 출력하였습니다.
'🧩PS > 🥈Nomal' 카테고리의 다른 글
[C/C++] 백준 11718번 - 그대로 출력하기 (0) | 2020.12.20 |
---|---|
[C/C++] 백준 1264번 - 모음의 개수 (0) | 2020.12.20 |
[C/C++] 백준 20291번 - 파일 정리 (0) | 2020.12.19 |
[C/C++] 백준 10830번 - 행렬 제곱 (2) | 2020.12.19 |
[C/C++] 백준 1300번 - K번째 수 (이분 탐색) (0) | 2020.12.19 |