-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx0106_StringCompression.cpp
More file actions
122 lines (94 loc) · 2.39 KB
/
Ex0106_StringCompression.cpp
File metadata and controls
122 lines (94 loc) · 2.39 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cassert>
using namespace std;
void InsertionSort(char* arr, int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) // <- while 사용
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int Count(char* arr, int n, char x)
{
int count = 0;
for (int i = 0; i < n; i++) // start index 사용
{
if (arr[i] == x)
count++;
}
return count;
}
int main()
{
// 문제
// - 어떤 알파벳에 몇 번 나오는지로 출력
// - 예시) aaabbccdddeeeff -> a3b2c2d3e3f2
// - 알파벳은 총 26 글자
// cout << int('a') << " " << int('z') << endl; // 97 122
// char arr[] = "ababcdfdceeefda";
// char arr[] = "a";
char arr[] = "ababcdfdceeedagsgdsgqreqgqw";
int n = sizeof(arr) - 1; // 마지막 안보이는 '\0' 제외
// 글자가 하나이상이라고 가정
// 글자가 없으면 if로 아예 수행을 안하도록 처리 가능
assert(n >= 1);
cout << arr << endl;
// 풀이 1. 모든 알파벳에 대해서 Count()
// 힌트: 소문자 알파벳 'a'~'z'는 int로는 97~122에 대응
// 단점: 없는 알파벳도 세야 한다.
// 표를 사용할 수도 있고 사용하지 않을 수도 있음
int table[26] = { 0 }; // 모든 값을 0으로 초기화
for (int i = 0; i < 26; i++)
{
// 힌트: char(i + 97)
// 표를 만들고 나중에 몰아서 출력하는 방법
// table[i] = ...
table[i] = Count(arr,n,char(i + 97));
// 표를 만들지 않고 직접 출력하는 방법
// ...
int count = Count(arr,n,char(i + 97)); // no hard coding!
if(Count(arr,n,char(i + 97)) > 0)
cout << char(i + 97) << count << flush;
}
cout << endl;
// 출력
for (int i = 0; i < 26; i++)
{
// ...
if(table[i] > 0) cout << char(i + 97) << table[i];
}
cout << endl << endl;
// 풀이 2. 정렬 후 찾기
// 여기서는 별도의 문자열을 만드는 것이 아니라 출력이 목표
// Table도 만들지 않음
InsertionSort(arr, n);
cout << arr << endl;
char c = arr[0];
int count = 1;
cout << c;
for (int i = 1; i < n; i++)
{
if (arr[i] == c)
{
// TODO: ...
count++;
}
else
{
// TODO: ...
cout << count;
c = arr[i];
count = 1;
cout << c;
}
}
cout << count << endl; // 마지막 count 출력
return 0;
}