๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋‚ด์šฉ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๋‚˜์ง€ ์•Š์„๊นŒ ํ–ˆ๋Š”๋ฐ, ํ†ต๊ณผ๋˜์—ˆ๋„ค์š”. ๋ฌธ์ œ๊ฐ€ ๊ทธ๋ฆฌ ์–ด๋ ต์ง„ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ํ’€์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <string>
using namespace std;

int solution(string s) {
	int answer = 10000;
	int extractNum = 1;

	while (extractNum <= s.length()) {
		string preToken = "";
		string compressedString = "";
		int repeatCount = 1;

		for (int i = 0; i < s.length(); i += extractNum) {
			string token = s.substr(i, extractNum);

			if (preToken.empty()) {
				preToken = token;
				continue;
			}

			if (preToken != token) {
				string strRepeatCount = repeatCount == 1 ? "" : to_string(repeatCount);
				compressedString += strRepeatCount;
				compressedString += preToken;

				// ์ตœ์†Ÿ๊ฐ’๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ๋Š” ๋ณผ ํ•„์š” ์—†์œผ๋ฏ€๋กœ ๋‹ค์Œ ๋ฃจํ”„๋กœ
				if (answer < compressedString.length())
					continue;

				preToken = token;
				repeatCount = 1;
				continue;
			}

			repeatCount++;
		}

		string strRepeatCount = repeatCount == 1 ? "" : to_string(repeatCount);
		compressedString += strRepeatCount;
		compressedString += preToken;
		answer = min(answer, (int)compressedString.length());
		extractNum++;
	}

	return answer;
}

 

 

728x90
๋ฐ˜์‘ํ˜•