-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRLECompare.cpp
More file actions
73 lines (66 loc) · 1.81 KB
/
RLECompare.cpp
File metadata and controls
73 lines (66 loc) · 1.81 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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int RLECompare(string& lhs, string& rhs) {
int p1 = 0, p2 = 0;
int ps1 = 0, ps2 = 0;
while (lhs[p1] != 0 && rhs[p2] != 0) {
while (isdigit(lhs[p1])) {p1++;}
while (isdigit(rhs[p2])) {p2++;}
if (isalpha(lhs[p1]) && isalpha(rhs[p2])) {
if (lhs[p1] == rhs[p2]) {
string num;
// num.append(lhs[ps1], rhs[p1 - 1]);//TODO
num.append(lhs, ps1, p1 - ps1);
int num1 = atoi(num.c_str());
num.clear();
num.append(rhs, ps2, p2 - ps2);
int num2 = atoi(num.c_str());
int sub = num1 - num2;
if (num1 == num2) {
ps1 = ++p1;
ps2 = ++p2;
} else if (num1 > num2) {
ps2 = ++p2;
while (isdigit(rhs[p2])) {p2++;}
string tem;
tem.append(rhs, ps2, p2 - ps2);
int n2 = atoi(tem.c_str());
if (lhs[p1] == rhs[p2] && sub == n2) {
ps1 = ++p1;
ps2 = ++p2;
} else {
return lhs[p1] - rhs[p2];
}
} else { // (num1 < num2)
ps1 = ++p1;
while (isdigit(lhs[p1])) {p1++;}
string tem;
tem.append(lhs, ps1, p1 - ps1);
int n2 = atoi(tem.c_str());
if (lhs[p1] == rhs[p2] && sub == n2) {
ps1 = ++p1;
ps2 = ++p2;
} else {
return lhs[p1] - rhs[p2];
}
}
} else {
return lhs[p1] - rhs[p2];
}
}
}
}
int main() {
std::cout << "Hello, World!" << std::endl;
// string s1 = "", s3A2B2 = "2A3B";
// string s1 = "33A", s2 = "31A2A";
// string s1 = "2A3B", s2 = "3A2B";
string s1 = "3A2B", s2 = "1B";
// string s2 = "33A", s1 = "31A2A";
int res = RLECompare(s1, s2);
cout << res;
return 0;
}