https://www.acmicpc.net/problem/9935
문제 설명
tmp1과 tmp2라는 문자열이 주어진다.
이 때, tmp1에 tmp2라는 문자열이 있으면 tmp1에서 사라진다. tmp1에 tmp2라는 문자열을 없앤 남아 있는 문자열을 출력하는 문제이다. 존재하지 않는 경우엔 "FRULA"라는 문자열을 출력한다.
주요 로직
1. 문자열의 substr() 과 erase() 함수를 사용한다.
-> tmp1의 i번째 문자가 tmp2의 마지막 문자와 동일한 경우에 substr() 함수로 확인해 tmp2 문자열이 있는 경우 삭제하도록 구현했다.
정답 코드
#include <bits/stdc++.h>
using namespace std;
char c;
string tmp1, tmp2, str;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> tmp1 >> tmp2;
c = tmp2[tmp2.size()-1];
for(int i=0; i<tmp1.size(); i++){
str += tmp1[i];
if(str.size() >= tmp2.size() && tmp1[i] == c){
string ss = str.substr(str.size()-tmp2.size());
if(ss == tmp2) str.erase(str.size()-tmp2.size(), tmp2.size());
}
}
if(str.size()) cout << str << "\n";
else cout << "FRULA\n";
return 0;
}
'PS' 카테고리의 다른 글
[백준] 1931번 회의실 배정 (C++) (1) | 2024.10.16 |
---|---|
[백준] 1781번 컵라면 (C++) (0) | 2024.10.15 |
[백준] 14729번 칠무해 (C++) (5) | 2024.10.14 |
[백준] 7569번 토마토 (C++) (2) | 2024.10.11 |
[백준] 7576번 토마토 (C++) (1) | 2024.10.10 |