-
7792. 반장 선출Algorithm/SWExpertAcademy 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; } }
'Algorithm > SWExpertAcademy' 카테고리의 다른 글
1247. [S/W 문제해결 응용] 3일차 - 최적 경로 (0) 2020.02.22 7088. 은기의 송아지 세기 (0) 2020.02.20 9480. 민정이와 광직이의 알파벳 공부 (0) 2020.02.18 7733. 치즈 도둑 (0) 2020.02.18 1211. [S/W 문제해결 기본] 2일차 - Ladder2 (0) 2020.02.17