Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

킹솔이

[프로그래머스/C++] 2020 KAKAO BLIND RECRUITMENT 문자열 압축 본문

Algorithm

[프로그래머스/C++] 2020 KAKAO BLIND RECRUITMENT 문자열 압축

킹솔이 2020. 1. 22. 19:19

 

#include<iostream>
#include <string>
#include <vector>

using namespace std;

int solution(string s) {
	int answer = 1000;
	//압축할 문자열갯수는 s.size()/2가 최대
	for (int i = 1; i <= s.size(); i++) {
		string str = "";
		vector<string> vec;
		int j = 0;
		while (j < s.size()) {
			vec.push_back(s.substr(j, i));
			j += i;
		}
		
		/*for (int m = 0; m < vec.size(); m++) {
			cout << vec[m]<<"  ";
		}*/
		//cout << endl;
		int a = 0;
		while (a < vec.size()) {
			/*if (a == vec.size() - 1 && ) {
				str.append(vec[vec.size() - 1]);
			}*/
			int temp = 1;
			int b = a + 1;
			while (b < vec.size() && vec[a] == vec[b] ) {
				temp++;
				b++;
			}
			if (temp == 1)
				str.append(vec[a]);
			else {
				str.append(to_string(temp));
				str.append(vec[a]);
			}
			//cout << str << endl;
			a += temp;
		}
		
		//if(vec[vec.size() - 1].size()<i)
		//	str.append(vec[vec.size()-1]);
		//cout << str << endl;
		//cout << str.length() << endl;
		if (answer > str.length())
			answer = str.length();
	}
	return answer;
}

int main() {
	string s;
	cin >> s;
	cout << solution(s);
	return 0;
}