hyelie
hyelie
Hyeil Jeong
       
글쓰기    관리    수식입력
  • 전체보기 (495)
    • PS (283)
      • Algorithm (28)
      • PS Log (244)
      • Contest (6)
      • Tips (5)
    • Development (52)
      • Java (14)
      • Spring (23)
      • SQL (2)
      • Node.js (2)
      • Socket.io (3)
      • Study (4)
      • Utils (4)
    • DevOps (36)
      • Git (5)
      • Docker (4)
      • Kubernetes (2)
      • GCP (3)
      • Environment Set Up (8)
      • Tutorial (12)
      • Figma (2)
    • CS (74)
      • OOP (7)
      • OS (24)
      • DB (2)
      • Network (24)
      • Architecture (0)
      • Security (2)
      • Software Design (0)
      • Parallel Computing (15)
    • Project (15)
      • Project N2T (5)
      • Project ASG (0)
      • Project Meerkat (1)
      • Model Checking (7)
      • Ideas (2)
    • 내가 하고싶은 것! (34)
      • Plan (16)
      • Software Maestro (10)
      • 취준 (8)
hELLO · Designed By 정상우.
hyelie

hyelie

PS/PS Log

22.04.19. 풀었던 문제들

1. 문자열 압축

입력 길이가 1일 때 예외가 있었다.

// 문자열 압축

#include <string>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

int solution(string s) {
    int length = s.length(), min_value = 9999999;
    
    // cl : 압축 단위(compress length). ceil을 쓴 이유는 문자열 길이가 1일 수 있기 때문임.
    for(int cl = 1; cl<=ceil((double)length/2); cl++){
        string compressed_s = "";
        for(int i = 0; i<length;){
            int repeat_num = 1;
            string unit_string = "";
            if(i + cl <= length){ // 문자열 검사를 할 수 있는 경우
                unit_string = s.substr(i, cl);
                i += cl;
                while(i + cl <= length){
                    string next_string = s.substr(i, cl);
                    if(unit_string != next_string) break;
                    repeat_num++;
                    i += cl;
                }
            } else { // 문자열 마지막 부분인 경우
                unit_string = s.substr(i, length-i);
                i += cl;
            }

            if(repeat_num > 1){ // 중복문자열이 존재하는 경우
                compressed_s += to_string(repeat_num) + unit_string;
            } else{ // 존재하지 않는 경우
                compressed_s += unit_string;
            }
        }
        min_value = min_value > compressed_s.length()? compressed_s.length() : min_value;
    }
    
    return min_value;
}

'PS > PS Log' 카테고리의 다른 글

22.04.30. 풀었던 문제들  (0) 2022.06.23
22.04.20. 풀었던 문제들  (0) 2022.06.23
22.04.17. 풀었던 문제들  (0) 2022.06.23
22.04.16. 풀었던 문제들 *** KMP 알고리즘  (0) 2022.06.23
22.04.15. 풀었던 문제들  (0) 2022.06.23
    hyelie
    hyelie

    티스토리툴바