문제 (level2, 2020 KAKAO BLIND RECRUITMENT)
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
#include <string>
#include <vector>
using namespace std;
int solution(string s) {
int len=s.length();
int min=len;//리턴값
for(int i=1; i<=len/2 ; i++) // i는 자른 단위
{
int block=0; //같은 문자열덩이의 총 갯수 (ex.2abc12dddf의 경우 2저장됨)
int num_count=0; //압축된 문자열에서 숫자의 갯수 (ex.2abc12dddf의 경우 3저장됨)
int remainder=len; //묶고 남은 부분의 길이 (ex.2abc12dddf의 경우 1저장됨)
bool is_first=true;
int temp=0; //num_count을 구하기 위한 임시변수
for(int j=i; j<=len-1;) //j는 현재 탐색중인 시작 인덱스의 위치
{
bool is_same=s.substr(j,i)==s.substr(j-i,i);
if(is_same)//연속된 같은 문자열이라면
{
if(is_first)
{
temp=2;
is_first=false;
block++;
}
else
temp++;
}
if((!is_same && temp!=0) || (is_same && j==len-i)) //연속되던 문자열덩어리가 끊겼을떄
{
is_first=true;
remainder-=temp*i;
string str_temp=to_string(temp);
num_count+=str_temp.length();
temp=0;
}
j+=i;
}
int temp_len=(i*block)+num_count+remainder;
if(temp_len<min)
min=temp_len;
}
return min;
}
이번 문제는 제가 깔끔하게 풀지 못한것 같아서 상세 풀이는 쓰지 않았씁니다 ..ㅠㅠ
일단 통과된다는거에 위안을..
'알고리즘 > 프로그래머스 문제풀이' 카테고리의 다른 글
[C++] 멀쩡한 사각형 (Lv.2) (0) | 2022.08.30 |
---|---|
[C++] 단체사진 찍기 (Lv.2) (0) | 2022.08.29 |
[C++] 카카오 프렌즈 컬러링북 (Lv.2) (0) | 2022.08.28 |
[C++] 프로그래머스-오픈채팅방 (Lv.2) (0) | 2022.08.24 |
[C++] 프로그래머스-두 큐 합 같게 만들기 (Lv.2) (0) | 2022.08.21 |