반응형
programmers.co.kr/learn/courses/30/lessons/60057?language=cpp
은근 오래걸렸다 .. 2시간?
2,3개씩 비교할때 문자열 전체크기보다 넘을때의 경우를 생각하느라 오래걸림
문자열이 총 10개인데 3/3/3/1이렇게 비교해야함 이럴경우
if (a >= s.size()) {
newStr = savedStr + selectedStr;
flag = 1;
break;
}
이렇게 처리함
원래 저기에서 바로 newStr에 더해줬더니 ..
newStr += cnt == 1 ? savedStr : to_string(cnt) + savedStr; // s.size()넘었을 경우 남은 str맨뒤에 추가 해줘야한다.
이부분에서도 더해지고 위에서도 더해져서 문제가 생겼음 > 그래서 savedStr로 넣어주고 for문 밖에서 처리되게 수정!
다양한 경우의 수를 생각해야한다.
사이즈가 10일때
2/2/2/2/2
3/3/3/1
5/5
딱 떨어지는 경우, 배열밖으로 나가는 경우 등등
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(string s) {
int answer = 0;
int minSize = 987654321;
for (int i = 1; i <= s.size(); i++) {
string savedStr;
string newStr;
int cnt = 0;
int start = 0;
int flag = 0;
for (int a = 0; a < i; a++) {
savedStr += s[a];
}
for (int j = 0; j < s.size(); j += i) {
string selectedStr;
for (int a = j; a < j + i; a++) {
if (a >= s.size()) {
savedStr = savedStr + selectedStr;
flag = 1;
break;
}
selectedStr += s[a];
}
if (savedStr == selectedStr) {
cnt++;
}
else if (!flag) {
newStr += cnt == 1 ? savedStr: to_string(cnt) + savedStr;
savedStr = selectedStr;
cnt = 1;
}
}
newStr += cnt == 1 ? savedStr : to_string(cnt) + savedStr; // s.size()넘었을 경우 남은 str맨뒤에 추가 해줘야한다.
int sz = newStr.size();
minSize = min(sz, minSize);
}
answer = minSize;
return answer;
}
반응형
'CS > 알고리즘' 카테고리의 다른 글
문자 형 변환 (0) | 2021.10.08 |
---|---|
프로그래머스 42862 (0) | 2021.06.05 |
프로그래머스 - 네트워크 (dfs) (0) | 2021.03.06 |
백준 19236 청소년 상어 (dfs ,시뮬) _ again (0) | 2021.02.20 |
백준 20056 마법사 상어와 파이어볼 (시뮬) (0) | 2021.02.15 |