262 words
1 minutes
[BOJ] HTML 에디터
TimeLimit | MemoryLimit | Condition | TAG |
---|---|---|---|
1s | 128MB | (0 ≤ B ≤ E ≤ TEXT의 길이, 1 ≤ TEXT의 길이 ≤ 200자) | Implementation |
엄… 이거 범위를 +1을 안해서 틀리는 이슈… 화이팅 다들.. ㅠ
오늘은 학교에서 푼거라서 영상이 없어유
정답 코드
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
using ll = long long;
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
void solution(int start, int end, string& input){
vector<string> tags;
bool state = false;
string current = "";
string result = "";
for(int i = 1; i < end; i++){
char c = input[i];
if(c == '<'){
state = true;
result+=current;
current="";
}else if(c == '>'){
if(!state){
current+=c;
continue;
}
if(current[0] =='/'){
if(i < start){
replace(result, "<"+tags.back()+">", "");
tags.pop_back();
current="";
state = false;
continue;
}
tags.pop_back();
}
else tags.push_back(current);
result += "<"+current +">";
current="";
state = false;
} else if(state || (!state && i >= start)) current+=c;
}
result+= current;
cout << result;
while(!tags.empty()){
string tag = tags.back(); tags.pop_back();
cout << "</" << tag << ">";
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int start, end;
string current;
while(1){
cin >> start >> end;
if(start == -1) break;
getline(cin, current);
solution(start+1, end+1, current);
cout << "\n";
}
return 0;
}
// <b></b> <a>a</a> <a></a>
// 7 17 <b></b> <a>a</a> <a></a>
[BOJ] HTML 에디터
https://compy07.github.io/Blog/posts/boj/4752/