-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ#04_Lab11.cpp
More file actions
94 lines (73 loc) · 2.22 KB
/
Q#04_Lab11.cpp
File metadata and controls
94 lines (73 loc) · 2.22 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
#include <iostream>
using namespace std;
class CustomHashMap {
static const int SIZE = 256;
int precedence[SIZE];
public:
CustomHashMap() {
for (int i = 0; i < SIZE; i++) {
precedence[i] = -1;
}
}
void setPrecedence(char key, int value) {
precedence[key] = value;
}
int getPrecedence(char key) {
return precedence[key];
}
};
int partition(char arr[], int low, int high, char pivot, CustomHashMap &hashMap) {
int i = low - 1;
for (int j = low; j < high; j++) {
if (hashMap.getPrecedence(arr[j]) < hashMap.getPrecedence(pivot)) {
i++;
swap(arr[i], arr[j]);
} else if (hashMap.getPrecedence(arr[j]) == hashMap.getPrecedence(pivot)) {
swap(arr[i + 1], arr[j]);
}
}
return i + 1;
}
void matchNutsAndBolts(char nuts[], char bolts[], int low, int high, CustomHashMap &hashMap) {
if (low < high) {
char pivot = bolts[high];
int partitionIndex = partition(nuts, low, high, pivot, hashMap);
partition(bolts, low, high, nuts[partitionIndex], hashMap);
matchNutsAndBolts(nuts, bolts, low, partitionIndex - 1, hashMap);
matchNutsAndBolts(nuts, bolts, partitionIndex + 1, high, hashMap);
}
}
int main() {
int n;
cout << "Enter the number of nuts and bolts: ";
cin >> n;
char nuts[n], bolts[n];
cout << "Enter the nuts: ";
for (int i = 0; i < n; i++) {
cin >> nuts[i];
}
cout << "Enter the bolts: ";
for (int i = 0; i < n; i++) {
cin >> bolts[i];
}
CustomHashMap precedenceMap;
precedenceMap.setPrecedence('#', 0);
precedenceMap.setPrecedence('$', 1);
precedenceMap.setPrecedence('%', 2);
precedenceMap.setPrecedence('&', 3);
precedenceMap.setPrecedence('*', 4);
precedenceMap.setPrecedence('@', 5);
precedenceMap.setPrecedence('^', 6);
precedenceMap.setPrecedence('~', 7);
matchNutsAndBolts(nuts, bolts, 0, n - 1, precedenceMap);
cout << "Matched Nuts and Bolts: " << endl;
for (int i = 0; i < n; i++) {
cout << nuts[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << bolts[i] << " ";
}
cout << endl;
return 0;
}