https://www.welcomekakao.com/learn/courses/30/lessons/60057
확인사항
숫자가 있으면 전부 1을 늘려주는 것으로 실수 했는데
공통 길이가 10일때 100일때 1000일때 처리해주면 된다.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct li {
string a;
int b;
};
vector <li> list;
int solve(int n, string s) {
list.clear();
int chk = 0;
string chkString = s.substr(0,n);
string nString;
int count = 1;
for (int i = n; i <= s.size(); i += n) {
nString = s.substr(i,n);
if (chkString.size() != nString.size()) {
list.push_back({ chkString,count });
list.push_back({ nString,1 });
break;
}
if (chkString == nString) {
count++;
}
else if (chkString != nString) {
list.push_back({ chkString,count });
count = 1;
chkString = nString;
}
}
for (int i = 0; i < list.size(); i++)
{
chk += list[i].a.size();
if (list[i].b > 1) {
if (list[i].b >= 10 && list[i].b <= 99) {
chk += 2;
}
else if (list[i].b >= 100 && list[i].b <= 999) {
chk += 3;
}
else if(list[i].b==1000){
chk += 4;
}
else {
chk += 1;
}
}
}
return chk;
}
int solution(string s) {
int answer = 0;
answer = s.size();
for (int i = 1; i <= s.size()/2+1; i++) {
int a = solve(i, s);
if (answer > a) {
answer = a;
}
}
return answer;
}
'개발자 > algorithm' 카테고리의 다른 글
2019 카카오 코딩테스트 2번 : 실패율 (c++) (0) | 2020.08.25 |
---|---|
2020 카카오 코딩테스트 3번 : 자물쇠와 열쇠 (c++) (0) | 2020.08.25 |
백준 1102번 : 발전소 (c++) (0) | 2020.08.21 |
백준 2098번 : 외판원 순회 (c++) (0) | 2020.08.21 |
백준 9202번 : Boggle (c++) (0) | 2020.08.21 |