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.08. 풀었던 문제들

1. [1차] 프렌즈4블록

쫄았는데 생각보다 풀 만 하다. 시간도 꽤 빨리 풀었고... 단순 구현 문제였다.

​

​

2. [3차] 압축

문자열과 map을 사용하는 문제다. 주어지는 대로 구현하면 되고, index가 잘 계산되었는지만 신경쓰면 된다.

// 압축

#include <string>
#include <vector>
#include <map>
#include <iostream>

using namespace std;

vector<int> solution(string msg) {
    // 단계 1
    map<string, int> dict;
    vector<string> alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    int dict_idx = 1;
    for(dict_idx = 1; dict_idx < 27; dict_idx++)
        dict[alphabet[dict_idx-1]] = dict_idx;
    
    vector<int> answer;
    int maxlen = 1, msg_length = msg.length();
    for(int i = 0; i<msg_length;){
        if(i + maxlen >= msg_length) maxlen = msg_length - i;
        // 단계 2
        for(int len = maxlen; len >= 1; len--){
            string substring = msg.substr(i, len); // 가장 긴 문자열 
            
            // 단계 3
            if(dict[substring] != 0){
                answer.push_back(dict[substring]);
                
                // 단계 4
                if(i + len < msg_length){
                    maxlen = max(maxlen, len + 1);
                    dict[msg.substr(i, len + 1)] = dict_idx++;
                }
                
                i += len;
                break;
            }
        }
    }
    
    
    return answer;
}

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

22.04.15. 풀었던 문제들  (0) 2022.06.23
22.04.09. 풀었던 문제들  (0) 2022.06.23
22.04.07. 풀었던 문제들  (0) 2022.06.23
22.04.06. 풀었던 문제들  (0) 2022.06.22
22.04.05. 풀었던 문제들  (0) 2022.06.22
    hyelie
    hyelie

    티스토리툴바