2966번: 찍기
문제 상근이, 창영이, 현진이는 역사와 전통을 자랑하는 Sogang ACM-ICPC Team에 가입하려고 한다. 하지만, 가입하려고 하는 모든 지원자는 C언어 필기시험을 통과해야 한다. 이들은 C언어를 할 줄 모른다. 따라서, 필기시험을 모두 찍으려고 한다. 상근이는 A, B, C, A, B, C, A, B, C, A, B, C, ...와 같이 찍어야 통과할 수 있다고 생각한다. 하지만, 창영이는 B, A, B, C, B, A, B, C, B, A, B
www.acmicpc.net
string 컨테이너와 반복자를 사용하여 손쉽게 해결한 문제였다.
Adrian의 찍는 순서는 "ABC", Bruno의 찍는 순서는 "BABC", Goran의 찍는 순서는 "CCAABB" 임을 알 수 있기 때문에
찍는 순서를 string에 저장하고 반복자를 한칸씩 증가시키는 방법으로 입력된 문자열과 비교하였다.
#include <iostream>
#include <string>
using namespace std;
int max(const int a, const int b)
{
return a > b ? a : b;
}
int max(const int a, const int b, const int c)
{
return a > max(b, c) ? a : max(b, c);
}
int main(void)
{
int len, AdrCnt = 0, BruCnt = 0, GoCnt = 0;
string problem, Adrian("ABC"), Bruno("BABC"), Goran("CCAABB");
cin >> len >> problem;
string::iterator AdrIter = Adrian.begin(), BruIter = Bruno.begin(), GoIter = Goran.begin();
for (int i = 0; i < len; i++)
{
if (problem[i] == *AdrIter++) AdrCnt++;
if (AdrIter == Adrian.end()) AdrIter = Adrian.begin();
if (problem[i] == *BruIter++) BruCnt++;
if (BruIter == Bruno.end()) BruIter = Bruno.begin();
if (problem[i] == *GoIter++) GoCnt++;
if (GoIter == Goran.end()) GoIter = Goran.begin();
}
int maxNum = max(AdrCnt, BruCnt, GoCnt);
cout << maxNum << endl;
if (AdrCnt == maxNum) cout << "Adrian" << endl;
if (BruCnt == maxNum) cout << "Bruno" << endl;
if (GoCnt == maxNum) cout << "Goran" << endl;
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
알고리즘 200일 프로젝트 - 32day
'알고리즘 > BOJ' 카테고리의 다른 글
백준 1074번: Z (2) | 2020.05.12 |
---|---|
백준 11729번: 하노이 탑 이동 순서 (0) | 2020.05.11 |
백준 4641번: Doubles (0) | 2020.05.06 |
백준 2858번: 기숙사 바닥 (0) | 2020.05.05 |
백준 1405번: 미친 로봇 (0) | 2020.05.03 |