daf

Kimbab1004·2024년 8월 17일
0

Algorithm

목록 보기
71/102
#include <iostream>
#include <vector>

using namespace std;

int n;
string s;
vector<char> v;
string dp[51];
vector<char> result;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	cin >> s;

	for (int i = 0; i < s.size(); i++) {
		v.push_back(s[i]);
	}
	
	dp[0] = v[0];
	result.push_back(v[0]);

	for (int i = 1; i < n; i++) {
		if (v[i] == 'P') {
			dp[i] = 'P';
			result.push_back(v[i]);
		}
		else if (v[i] == 'S') {
			if (dp[i - 1] == "P") {
				dp[i] = "PS";
			}
			else {
				dp[i] = 'S';
			}
			result.push_back(v[i]);
		}
		else if (v[i] == '4' || v[i] == '5') {
			if (dp[i - 1] == "PS") {
				dp[i] = "PS";
				continue;
			}
			else {
				result.push_back(v[i]);
			}
		}
		else {
			result.push_back(v[i]);
		}
	}

	for (int i = 0; i < result.size(); i++) {
		cout << result[i];
	}
	return 0;
}

0개의 댓글