티스토리 뷰
문제링크: koi.or.kr/assets/koi/2020/1/problems/m2-problems.pdf 또는 www.acmicpc.net/problem/19941
input
식탁의 길이 N 과 햄버거를 선택할 수 있는 거리 k
사람과 햄버거의 위치가 P(사람) H(햄버거) 로 이루어지는 길이 N 의 문자열
output
햄버거를 먹을수 있는 최대 사람 수
solution
P 와 H 로 이루어진 문자열을 처음부터 탐색한다.
P 가 나오면 그 지점(i)을 기준으로 i-k 부터 i+k 까지 탐색한다.
탐색중에 햄버거가 발견되었다면 햄버거를 H나 P가 아닌 다른 문자로 바꾸고 햄버거를 먹을 수 있는 사람 수를 1 더해준다.
code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int main() | |
{ | |
int N, k, cnt = 0; | |
char c; | |
vector<char> ph; | |
cin >> N >> k; | |
for(int i = 0; i < N; i++){ | |
cin >> c; | |
ph.push_back(c); | |
} | |
for(int i = 0; i < N; i++){ | |
if(ph[i] != 'P')continue; | |
for(int j = i - k; j <= i + k; j++){ | |
if(0 <= j && j < N && ph[j] == 'H'){ | |
ph[j] = '0'; | |
cnt++; | |
break; | |
} | |
} | |
} | |
cout << cnt; | |
return 0; | |
} |
'algorithm > 백준' 카테고리의 다른 글
[python] 백준 1001번: A-B (0) | 2021.05.01 |
---|---|
[python] 백준 1000번: A+B (0) | 2021.05.01 |
[C++] 백준 10870번 : 피보나치 수 5 (0) | 2021.03.13 |
[C++] 백준 2748번 : 피보나치 수 2 (0) | 2021.03.13 |
피보나치 수 시리즈 (0) | 2021.03.13 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 괄호
- c++
- programmers
- 프로그레머스
- forensic
- 백준
- web
- FIBO
- 나머지
- python
- 2
- 더하기
- 쇠막대기
- 파이썬
- 21758
- 1
- HackCTF
- 넓이
- openCV
- 👼
- 피보나치
- boj
- 7567
- 꿀따기
- math
- 직업군 추천
- 다익스트라
- 카카오 2021 블라인드 테스트
- 스택
- 4
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함