-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMobileCasanova.cpp
97 lines (73 loc) · 1.57 KB
/
MobileCasanova.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
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
#include <bits/stdc++.h>
template<typename T> T gcd(T a, T b) {
if(!b) return a;
return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
return a * b / gcd(a, b);
}
template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }
using namespace std;
typedef long long Int;
typedef unsigned uint;
const int MAXN = 100010;
int N;
string P[MAXN];
Int toInt(string arg) {
Int ans = 0LL;
for (int i = 0; i < (int) arg.size(); i++) {
ans = ans * 10 + (arg[i] - '0');
}
return ans;
}
void addRange(vector<string>& arg, int len, int pos) {
string l = P[pos - len];
string r = P[pos];
int id = 0;
string suffix = "";
while (id < (int) l.size() && l[id] == r[id]) {
id += 1;
}
while (id < (int) l.size()) {
suffix += r[id];
id += 1;
}
if (!arg.empty() && arg.back() == l) {
arg.pop_back();
}
if (suffix == "") {
arg.push_back(r);
} else {
arg.push_back(l + "-" + suffix);
}
}
int main(void) {
int test = 1;
while (cin >> N && N != 0) {
vector<string> ans;
Int last = 0, len = 0;
for (int i = 0; i < N; i++) {
cin >> P[i];
Int now = toInt(P[i]);
if (last + 1 == now) {
len += 1;
} else {
if (len > 0) {
addRange(ans, len, i - 1);
}
ans.push_back(P[i]);
len = 0;
}
last = now;
}
addRange(ans, len, N - 1);
cout << "Case " << test++ << ":" << endl;
for (int i = 0; i < (int) ans.size(); i++) {
cout << ans[i] << endl;
}
cout << endl;
}
return 0;
}