버블모임 문제
이 문제는 문자열을 처리하여 특정 패턴을 검사하는 문제입니다. 문자열 중 'o'와 'O'의 조합을 통해 새로운 문자열을 생성하는 로직이 핵심입니다.
코드 설명:
입력 문자열을 처리하는 함수는 다음과 같이 작동합니다:
- 스택 자료구조를 사용하여 문자를 임시 저장합니다.
- 'o'와 'O'의 조합을 통해 새로운 문자열을 형성합니다.
- 최종적으로 스택에 남은 문자들을 출력합니다.
#include <algorithm>
#include <vector>
#include <stack>
#include <cstring>
#include <cstdio>
using namespace std;
int main() {
char s[105];
vector<char> result;
stack<char> temp;
while (scanf("%s", s) != EOF) {
result.clear();
temp.empty();
for (int i = 0; i < strlen(s); ++i) {
if (temp.empty()) {
temp.push(s[i]);
continue;
}
if (temp.top() == 'o' && s[i] == 'o') {
temp.pop();
if (temp.empty() || temp.top() != 'O') {
temp.push('O');
} else {
temp.pop();
}
} else if (s[i] == 'O' && !temp.empty() && temp.top() == 'O') {
temp.pop();
} else {
temp.push(s[i]);
}
}
while (!temp.empty()) {
result.push_back(temp.top());
temp.pop();
}
reverse(result.begin(), result.end());
for (char c : result) {
printf("%c", c);
}
printf("\n");
}
return 0;
}
플러그인 문제
이 문제는 문자열에서 반복되는 인접 문자를 제거하는 문제입니다. 제거 작업은 연속적으로 수행됩니다.
코드 설명:
입력 문자열을 처리하는 함수는 다음과 같이 작동합니다:
- 스택 자료구조를 사용하여 문자를 임시 저장합니다.
- 인접한 동일 문자를 발견하면 제거합니다.
- 최종적으로 스택에 남은 문자들을 출력합니다.
#include <vector>
#include <stack>
#include <cstring>
#include <cstdio>
using namespace std;
int main() {
char s[200000];
vector<char> result;
stack<char> temp;
scanf("%s", s);
temp.push(s[0]);
for (int i = 1; i < strlen(s); ++i) {
if (temp.empty()) {
temp.push(s[i]);
continue;
}
if (temp.top() == s[i]) {
temp.pop();
} else {
temp.push(s[i]);
}
}
while (!temp.empty()) {
result.push_back(temp.top());
temp.pop();
}
reverse(result.begin(), result.end());
for (char c : result) {
printf("%c", c);
}
printf("\n");
return 0;
}