fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 21287 (C++) Färgrobot
최초 업로드: 2025-11-13 16:34:58
최근 수정 시간: 2025-11-13 16:34:58
게시자: rlatjwls3333
카테고리: 백준
조회수: 10
# [Bronze I] Färgrobot [문제 링크](https://www.acmicpc.net/problem/21287) ## 문제 설명 <p>A robot can move along a row of colored squares. Every square can be either red, green or blue. A command to the robot is a color. The robot responds by moving to the right until it stands on the color specified by the command. Write a program that, for a given sequence of squares and a number $N$, writes out the sequence of $N$ commands that makes the robot go as far to the right as possible.</p> <p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/5f47d960-bb1b-480d-b202-56fc94d8f362/-/preview/" style="width: 510px; height: 97px;"></p> <p style="text-align: center;">Figure 1: An illustration of the third sample case.</p> ## 입력 <p>The first line containes an integer $N$, the number of commands to give to the robot. The second line contains a string of letters, R , G, or B, which specifies the color of each square in the row, from left to right. The string contains less than 200 characters, and is always sufficiently long that no sequence of $N$ commands may cause the robot to reach outside the row of squares.</p> ## 출력 <p>The program should output a string of $N$ characters, each being R, G, or B, which constitutes the sequence of commands you should give to the robot to make it move as far as possible along the row of squares. If there are several sequences that accomplish this, you may output anyone of them.</p> ## 풀이 한 지점에서 세 색 중 가장 마지막으로 등장하는 색으로 점프하는 것이 가장 멀리 점프하는 것입니다. 아무리 멀리 점프해도 밖을 벗어나지 않는다고 지문에 나와있으니 이를 N번 반복하면 됩니다. ``` c++ #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; string s; cin >> n >> s; bool r=0, g=0, b=0; for(char ch:s) { if(ch=='R') r=1; else if(ch=='G') g=1; else b=1; if(r && g && b) { cout << ch; r=g=b=0; if(--n==0) return 0; } } } ```