개인공부용123 프로그래밍 블로그

AlgoSpot) QUADTREE 본문

알고리즘문제

AlgoSpot) QUADTREE

개인공부용123 2017. 1. 21. 21:22

문제 : https://algospot.com/judge/problem/read/QUADTREE


문자열을 입력받았을떄 이 문자를 상하로 뒤집은 문자열을 출력하는 문제입니다.

분할 정복을 이용해서 문제를 풀었습니다.


*string의 begin함수는 r-value이므로 함수의 인자에 r-value를 넣으면 알고스팟 컴파일러에서 에러가 나왔습니다.

그러므로 다음 코드와 같이 l-value로 지정을 해준후 인자로 넣어주면 제대로 컴파일이 됩니다.


r-value와 l-value 참조 : http://blog.mimu.me/understand-rvalue-reference-ko.html



#include <iostream>
#include <string>

using namespace std;

string str;
// 1 2
// 3 4 이형태로 만듬
string ReverseTree(std::string::iterator& it) {
	char c = *it;
	++it;

	if (c == 'w' || c == 'b') return string(1,c); // string형태로 c문자를 1개 채워넣는다.

	string first = ReverseTree(it);
	string second = ReverseTree(it);
	string third = ReverseTree(it);
	string fourth = ReverseTree(it);

	return string("x") + third + fourth + first + second;
}

string ReverseTree2(int& num) {
	char c = str[num];
	++num;
	
	if (c == 'w' || c == 'b') return string(1,c); // string형태로 c문자를 1개 채워넣는다.
	
	string first = ReverseTree2(num);
	string second = ReverseTree2(num);
	string third = ReverseTree2(num);
	string fourth = ReverseTree2(num);
	
	return string("x") + third + fourth + first + second;
}

int main() {
	std::ios::sync_with_stdio(false);
	freopen("text.txt", "r", stdin);

	int C;
	
	cin >> C;

	while (C--) {
		cin >> str;

		string::iterator it = str.begin();
		cout << ReverseTree(it) << endl;

		/*int n = 0;
		cout << ReverseTree2(n) << endl;*/

		str.clear();
	}

	return 0;
}


'알고리즘문제' 카테고리의 다른 글

AlgoSpot) DARPA  (0) 2017.01.31
AlgoSpot) ALLERGY  (0) 2017.01.22
AlgoSpot) STRJOIN  (0) 2017.01.16
AlgoSpot) LUNCHBOX  (12) 2017.01.14
AlgoSpot) MATCHORDER  (0) 2017.01.14