-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy path1078. 字符串压缩与解压.cpp
37 lines (37 loc) · 1.04 KB
/
1078. 字符串压缩与解压.cpp
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
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
string compress(const string& s) { //压缩
string r;
for (gg i = 0, j = 0; i < s.size(); i = j) {
//找到下标i之后第一个与s[i]不同的字符的下标
j = s.find_first_not_of(s[i], i);
if (j == -1)
j = s.size();
//压缩当前字符
r += (j - i == 1 ? "" : to_string(j - i)) +
string(1, s[i]);
}
return r;
}
string decompress(const string& s) { //解压
string r;
for (gg i = 0, j = 0; i < s.size(); i = j + 1) {
//找到下标i之后第一个非数字字符的下标
j = s.find_first_not_of("0123456789", i);
//解析字符s[j]的个数
gg k = j - i == 0 ? 1 : stoll(s.substr(i, j - i));
//解压s[j]字符
r += string(k, s[j]);
}
return r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string fi, si;
getline(cin, fi);
getline(cin, si);
cout << (fi == "C" ? compress(si) : decompress(si));
return 0;
}