-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path常见算法.cpp
208 lines (197 loc) · 3.82 KB
/
常见算法.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 1. 带过期时间LRU
class LRU{
public:
LRU(int t, int timeO):cap(t), timeout(timeO){};
int get(int key, int now){
int t = now + timeout;
if(kv.count(key)){
touch(key, t);
return kv[key];
}else{
return -1;
}
}
void put(int key, int val, int now){
int t = now + timeout;
if(!kl.count(key)&&arr.size()==cap){
kv.erase(arr.back());
kl.erase(arr.back());
kt.erase(arr.back());
arr.pop_back();
}
touch(key, t);
kv[key] = val;
}
int out(int key, int now){
int count = 0;
while(!arr.empty()&&kt[arr.back()] < now){
kv.erase(arr.back());
kl.erase(arr.back());
kt.erase(arr.back());
arr.pop_back();
count++;
}
return count;
}
private:
list<int> arr;
int cap, timeout;
unordered_map<int, list<int>::iterator> kl;
unordered_map<int, int> kv;
unordered_map<int, int> kt;
int touch(int key, int t){
if(kl.count(key)){
arr.erase(kl[key]);
}
arr.push_front(key);
kl[key] = arr.begin();
kt[key] = t;
}
}
// 生产、消费者问题
#define int asph
asph mutex = 1;
asph empty = 100;
asph full = 0;
void producer(){
while(1){
P(&empty);
P(&mutex);
Item* item = new Item();
vec.push_back(item);
V(&mutex);
V(&full);
}
}
void consumer(){
while(1){
P(&full);
P(&mutex);
Item* item = vec.top();
vec.pop();
V(&mutex);
V(&full);
}
}
// 读写者问题
#define int asph
asph read = 1;
asph write = 1;
int count = 0;
void _read(){
P(&read);
count++;
if(count == 1) P(&wirte);
V(&read);
read();
P(&read);
count--;
if(count==0) V(&write);
V(&read);
}
void _write(){
P(&wirte);
wirte();
V(&write);
}
// 哲学家进餐问题
#define int asph
asph chp[] = {1,1,1,1,1};
asph mutex = 1;
void eat(i){
while(True){
P(&mutex);
P(chp[i]);
p(chp[(i+1)%5]);
V(&mutex);
_eat(i);
V(chp[i]);
V(chp[(i+1)%5]);
}
}
// LCA问题
TreeNode* findLCA(TreeNode* root, TreeNode* left, TreeNode* right){
if(root==NULL||root==left||root==right) return root;
TreeNode* left = findLCA(root->left, left, right);
TreeNode* right = findLCA(root->right, left, right);
if(left==NULL&&right==NULL) return root;
return left==NULL?right:left;
}
struct Node{
Node* next;
int key;
int val;
Node(int k, int v):key(k),val(v),next(NULL){};
}
// hash算法
class HashMap{
public:
HashMap(int n){
vec.resize(n);
}
void put(int key, int val){
int index = getIndex(key);
if(vec[index].empty()){
Node* node = new Node(key, val);
temp.push_back(node);
}else{
Node* temp = vec[index];
Node* pre = search(temp, key);
if(pre==NULL){
temp->val = val;
}else if(pre->next==NULL){
Node* temp = new Node(key, val);
pre->next = temp;
}else{
pre->next->val = val;
}
}
}
int get(int key){
int index = getIndex(key);
Node* temp = vec[index];
Node* pre = search(temp, key);
if(pre==NULL){
return temp->val;
}else if(pre->next==NULL){
return -1;
}else{
return pre->next->val;
}
}
void del(int key){
int index = getIndex(key);
Node* temp = vec[index];
Node* pre = search(temp, key);
if(pre==NULL){
vec[index] = temp->next;
delete(temp);
temp=NULL;
}else if(pre->next==NULL){
printf("Error!");
exit();
}else{
Node* old = pre->next;
pre->next = pre->next->next;
delete(old);
old = NULL;
}
}
Node* search(Node* root, int key){
Node* pre = NULL;
while(root!=NULL){
if(root->key==key) break;
pre = node;
node = node->next;
}
return pre;
}
int hash(int key){
return (key.hash()>>16)^key.hash();
}
int getIndex(int key){
reurn hash(key)%vec.size();
}
private:
vector<Node*> vec;
}