Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- pycon
- 트라이 #trie #알고리즘
- 튜플
- bulk update
- 크레인 인형뽑기 게임
- Open API
- 불량 사용자
- Python
- trie
- 티스토리 open api
- CleanCode
- 알고리즘
- 보행자 천국
- 가사 검색
- 트라이
- 호텔 방 배정
- Tistory
- 프로그래머스
- 징검다리 건너기
- Spring Boot
- 카카오 인턴
- 티스토리
- jdbc
Archives
- Today
- Total
택시짱의 개발 노트
[프로그래머스] 문자열 압축 본문
링크
https://programmers.co.kr/learn/courses/30/lessons/60057
풀이
1개 이상 단위로 문자열을 잘라 압축하여 표현한 문자열 중 가장 짧은 것의 길이를 구하는 문제인데.
문자열의 길이가 1000 이하로 정해져 있기 때문에 o(n^2)의 시간 복잡도로 풀리는 문제인걸 알 수 있었다.
주어진 문자열 전체를 substr 함수를 이용하여 압축하고자 하는 길이만큼 비교하여 압축을 진행하였고
압축이 완성되었을 때 길이를 비교하여 결과를 찾아내었습니다.
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<set>
#include<map>
#include<cstring>
#include<functional>
#include<cmath>
#define SIZE 1010
const int INF = 2000000000;
using namespace std;
typedef long long int ll;
int solution(string s) {
int s_size = s.size();
int res = s_size;
for (int i = 1; i <= s_size / 2; i++) {
int word_cnt = 1;
string tmp = "";
string left, right;
int j;
for ( j = i; j < s_size; j+=i) {
left = s.substr(j - i, i);
right = s.substr(j, i);
if (left == right) {
word_cnt++;
}
else if (left != right) {
if (word_cnt != 1) {
tmp += to_string(word_cnt);
}
tmp += left;
word_cnt = 1;
}
}
if (word_cnt != 1) {
tmp += to_string(word_cnt);
tmp += left;
}
else if (word_cnt == 1) {
tmp += right;
}
res = min(res,(int) tmp.size());
}
return res;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
string s; cin >> s;
cout << solution(s);
}
반응형
'알고리즘' 카테고리의 다른 글
[프로그래머스] 자물쇠와 열쇠 (0) | 2020.03.08 |
---|---|
[프로그래머스] 괄호 변환 (0) | 2020.03.08 |
[프로그래머스] 등굣길 (0) | 2020.03.05 |
[프로그래머스] 체육복 (0) | 2020.03.05 |
[프로그래머스] 보행자 천국 (0) | 2020.02.25 |
Comments