※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 5345번 문제인 PLU Count이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/5345 

 

5345번: PLU Count

Given a text string you must find the number of non-interleaved occurrences of PLU in the string. The letters P, L, U must occur in order, but capitalization is not important and the letters need not be consecutive. For example the string “pppxLLLxuuu”

www.acmicpc.net

주어지는 문자열을 첫 문자부터 차례로 읽어나가며 "PLU"를 새로 구성하기 위해 필요한 다음 글자를 발견할 때마다 하나씩 이어나가는 것을 시도하면서 문제를 해결하자.

 

아래 구현의 target과 같은 배열을 이용하면 구현을 한결 편하게 할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <string>
using namespace std;

int T;
string s;
char target[3] = { 'p','l','u' };

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> T;
	getline(cin, s);
	while (T--) {
		getline(cin, s);
		int cnt = 0, turn = 0;
		for (auto& l : s) {
			l = tolower(l);
			if (target[turn] == l) {
				turn++;
				if (turn == 3) turn = 0, cnt++;
			}
		}

		cout << cnt << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 21771 // C++] 가희야 거기서 자는 거 아니야  (0) 2022.12.23
[BOJ 12791 // C++] Starman  (0) 2022.12.23
[BOJ 6889 // C++] Smile with Similes  (0) 2022.12.22
[BOJ 5343 // C++] Parity Bit  (0) 2022.12.22
[BOJ 6830 // C++] It's Cold Here!  (0) 2022.12.22

+ Recent posts