Algorithm/SWExpertAcademy
7792. 반장 선출
1일1코딩
2020. 2. 20. 00:20
LEVEL D4
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWsBNHuqMMADFARG
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
각 알파벳 별로 bitmask를 사용하였다.
현재 알파벳을 or 연산을 통해 더해나가면서 &연산을 통해 기존에 없을 경우에만 카운트를 증가시켜 알파벳 종류를 찾아 최대 갯수가 되면 정답을 체크하는 방식으로 문제에 접근하였다.
소스코드
더보기
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int N, T;
int map[26];
int answer;
int answerIdx;
void initMap()
{
for (int i = 0; i < 26; i++) {
map[i] = 0x01 << i;
}
}
int main()
{
initMap();
cin >> T;
for (int tc = 1; tc <= T; tc++) {
cin >> N;
cin.ignore();
string str;
string answerStr;
answer = 0;
answerIdx = 0;
for (int i = 0; i < N; i++) {
getline(cin, str);
int sum = 0;
int count = 0;
for (int j = 0; j < str.length(); j++) {
if (str[j] == ' ') continue;
if (!(sum & map[str[j] - 'A'])) {
count++;
}
sum |= map[str[j] - 'A'];
}
if (count > answer) {
answer = count;
answerIdx = i;
answerStr = str;
}
else if (count == answer) {
if (answerStr.compare(str) > 0) {
answerIdx = i;
answerStr = str;
}
}
}
cout << "#"<< tc << " " << answerStr << endl;
}
}