- String 관련 함수
-
// 문자열 자르기 string tok = s.substr(0, i); // 문자열 합치기 string new_str; new_str += to_string(cnt); // int to string new_str += tok;
-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <iostream> | |
using namespace std; | |
int solution(string s) { | |
int answer = 0; | |
int length = s.length(); | |
string new_str; | |
answer = length; | |
for (int i=1; i <= length/2; i++) | |
{ | |
string tok = s.substr(0, i); // 자를 단위 | |
string left_str = s.substr(i); // 자르고 남은 문자열 | |
string new_str; | |
int tok_cnt = length/i; | |
int cnt = 1; // tok 과 중복되는 값 | |
bool br = false; | |
for (int j = 1; j < tok_cnt; j++) | |
{ | |
if (tok == left_str.substr(0, i)) // tok 과 중복 | |
{ | |
br = true; | |
cnt++; | |
} | |
else // 다음 tok | |
{ | |
br = false; | |
if (cnt > 1) | |
new_str += to_string(cnt); | |
new_str += tok; | |
tok = left_str.substr(0, i); | |
cnt = 1; | |
} | |
if (j < tok_cnt-1) | |
left_str = left_str.substr(i); | |
} | |
if (br) | |
{ | |
new_str += to_string(cnt); | |
new_str += tok; | |
if (length % i) | |
new_str += s.substr(length - length%i); | |
} | |
else | |
{ | |
new_str += s.substr(i*(tok_cnt-1)); | |
} | |
if (answer > new_str.length()) | |
answer = new_str.length(); | |
} | |
return answer; | |
} |
나만 오래 걸리나bom
'알고리즘 공부' 카테고리의 다른 글
Leet code 가입 (0) | 2021.09.16 |
---|---|
[C++] vector 클래스 - 2차원 벡터 초기화 (0) | 2021.07.01 |
프로그래머스(#1844) - 게임 맵 최단거리도움말(JAVA) (0) | 2021.06.30 |
[JAVA] Queue (0) | 2021.06.29 |
프로그래머스(#17681) - 비밀지도도움말 (JAVA) (0) | 2021.06.29 |