Koala - 15기/코딩테스트 준비 스터디

[백준/C++] 17413번: 단어 뒤집기 2

소코기 2024. 8. 21. 15:50
  • 문제

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

  • 코드
#include <iostream>
#include <vector>
#include <math.h>
#include<string>
using namespace std;

int main()
{
	string s;
	getline(cin, s);
	int intag = 0;
	string ans = "";
	string word = "";

	for (int i = 0; i < s.length(); i++) {
		if (s[i] == ' ') {
			if (intag != 1) {
				ans += word;
				word = "";
			}
			ans += s[i];
		}
		else if (s[i] == '<') {
			ans += word;
			word = "";
			ans += s[i];
			intag = 1;
		}
		else if (s[i] == '>') {
			ans += s[i];
			intag = 0;
		}
		//숫자나 알파벳
		else {
			if (intag == 1) {
				ans += s[i];
			}
			else {
				word = s[i] + word;
				
			}
		}
	}
	ans += word;
	cout << ans << endl;
	
	return 0;
}
  • 알고리즘 분류 : 구현
  • 문제 해설 : 태그 안일 때는 체크해주면서 문자열을 더해주면 되는 문제 문자열을 거꾸로 더하고 싶을 때는 더하는 문자열에 기존에 더해줬던 문자열 변수를 더하고 그냥 더하고 싶을 때는 기존에 더해줬던 문자열에 더하는 문자열을 더하면(?) 된다.