반응형

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

 

16120번: PPAP

첫 번째 줄에 문자열이 주어진다. 문자열은 대문자 알파벳 P와 A로만 이루어져 있으며, 문자열의 길이는 1 이상 1,000,000 이하이다.

www.acmicpc.net

 

순서대로 문자를 스택에 담아주면서 최근 네 개의 문자가 'P', 'A', 'P', 'P' 일 때, 스택의 탑 포인터를 조정시켜줍니다.

마지막에 스택에 담긴 문자가 'P' 하나라면 PPAP 문자열입니다.

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

string str;
char stk[1000003] = { 0 };
int top = 1;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> str;
	for (int i = 0; i < str.size(); i++) {
		stk[++top] = str[i];
		if (stk[top] == 'P' && stk[top - 1] == 'A' && stk[top - 2] == 'P' && stk[top - 3] == 'P') {
			top -= 3;
		}
	}
	if (top == 2 && stk[top] == 'P') {
		cout << "PPAP";
	}
	else {
		cout << "NP";
	}
}
반응형

'Algorithm' 카테고리의 다른 글

백준 2800 : 괄호 제거  (0) 2021.11.17
백준 19238 : 스타트 택시  (0) 2021.11.17
백준 7490 : 0 만들기  (0) 2021.11.17
백준 1005 : ACM Craft  (0) 2021.11.17
백준 3665 : 최종 순위  (0) 2021.11.17

+ Recent posts