본문 바로가기

대학교 2-1/문해기

카카오 입사시험_문자열 압축: 문제 1-2 (발전)

위의 문제에 제한사항 3을 제외하도록 프로그램을 수정하라. 즉, 반복 횟수에 제한이 없다.

 

예 #1

ababababababababababababc <-입력

12abc<-출력

5<-출력

 

버전1) 시험장에서 푼 것 응용 버전

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
// 두 개의 문자들 연속에 대한 축약
int solution2(char* s)
{
    int count = 1;
    char flag[1001= { 0 };
    int j = 0;
    int digit;
    int len = 0;
 
    for (int i = 0; i < strlen(s); i = i + 2) {
        if (s[i] == s[i + 2&& s[i + 1== s[i + 3])
            count++;
        else {
            if (count > 1) {
                if (count >= 10) {
                    digit = calTenMultiple(count);
                    do {
                        flag[j] = (count / digit) + 48;
                        count = count % digit;
                        digit = digit / 10;
                        j++;
                    } while (count > 0);
                }
                else {//9까지
                    flag[j] = (count + 48);
                    j++;
                }
            }
            flag[j] = s[i];
            j++;
            flag[j] = s[i + 1];
            j++;
            count = 1;
        }
    }
    flag[j] = '\0';
    printf("%s", flag);
    printf("\n");
    for (int k = 0; flag[k] != '\0'; k++) {
        len++;
    }
    return len;
}
int calTenMultiple(int n) // n이 99면 10, 999면 100, 9999면 1000을 반환하는 함수
{
    int result = 1;
    while (n != 0) {
        n /= 10;
        result *= 10;
    }
    return result / 10;
}
int main(void)
{
    char s[1001];
    scanf("%s", s);
    printf("%d\n", solution2(s)); // 문제-2)
}
cs

 

 

버전2)

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
int solution(char* s) //ababccdddd라 하면 ab가 str1, ab가 str2
{
    int i, j = 0;
    int len;
    int q;
    int k;
    char temp[1000= "";
    char str1[5001= "";
    char str2[5001= "";
    int digit;
 
    strncpy(str1, s, 2);//strncpy로 초기값 설정
 
    len = 1;
    for (q = 2; q < strlen(s); ) { //달라지고
        for (k = 0; k < 2; k++, q++) {
            str2[k] = s[q];
        }
 
        if (strcmp(str1, str2) == 0//str1과 str2가 같으면
            len++;
        else { //다르면
            if (len > 1) {
                if (len >= 10) {
                    digit = calTenMultiple(len);
                    do {
                        temp[j] = (len / digit) + 48;
                        len = len % digit;
                        digit = digit / 10;
                        j++;
                    } while (len > 0);
                }
                else
                    temp[j++= len + '0'//숫자를 문자로 변환
            }
 
            strcat(temp, str1, 2);//strcat으로 문자를 넣기
            j = j + 2;
 
            strcpy(str1, str2); //strcpy
            len = 1//길이 초기화
        }
    }
 
    if (len > 1) {
        if (len >= 10) {
            digit = calTenMultiple(len);
            do {
                temp[j] = (len / digit) + 48;
                len = len % digit;
                digit = digit / 10;
                j++;
            } while (len > 0);
        }
        else
            temp[j++= len + '0'//숫자를 문자로 변환
    }
 
    strcat(temp, str1, 2);//strcat으로 문자를 넣기
    j = j + 2;
 
    temp[j] = '\0';
    printf("%s\n", temp);
 
    return strlen(temp);
}
int calTenMultiple(int n) // n이 99면 10, 999면 100, 9999면 1000을 반환하는 함수
{
    int result = 1;
    while (n != 0) {
        n /= 10;
        result *= 10;
    }
    return result / 10;
}
int main(void)
{
    char s[1001];
    scanf("%s", s);
    printf("\n%d\n", solution(s)); // 문제-2)
}
cs

 

 

버전3) 가장 깔끔하고 완벽한 버전 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int solution(char* s) //ababccdddd라 하면 ab가 str1, ab가 str2
{
    int i, j = 0;
    int len;
    int q;
    int k;
    char temp[1000= "";
    char str1[501= "";
    char str2[501= "";
    char flag[20]; //임시 버퍼
    int digit;
 
    strncpy(str1, s, 2);//strncpy로 초기값 설정
 
    len = 1;
    for (q = 2; q < strlen(s); ) { //달라지고
        for (k = 0; k < 2; k++, q++) {
            str2[k] = s[q];
        }
 
        if (strcmp(str1, str2) == 0//str1과 str2가 같으면
            len++;
        else { //다르면
            if (len > 1) { //sprintf로 숫자를 문자열로 바꾸고 strcat로 
                sprintf(flag, "%d", len);
                strcat(temp, flag);
            }
 
            strcat(temp, str1, 2);//strcat으로 문자를 넣기
 
            strcpy(str1, str2); //strcpy
            len = 1//길이 초기화
        }
    }
 
    if (len > 1) {
        sprintf(flag, "%d", len);
        strcat(temp, flag);
    }
 
    strncat(temp, str1, 2);//strcat으로 문자를 넣기
    
    printf("%s\n", temp);
 
    return strlen(temp);
}
int main(void)
{
    char s[1001];
    scanf("%s", s);
    printf("\n%d\n", solution(s)); // 문제-2)
}
cs