600 粉
手写部分 STL 容器,优化普通 quicksort。
map<Key, Value> m; // 完成
pair<T1, T2> p; // 完成
hash_table<Key Value> h; // 散列表
Stack<Item> stack; // 完成
Queue<Item> queue; // 完成map
#include <stdio.h>
#include <stdlib.h>
const bool Red = true, Black = false;
template<typename Key, typename Value>
class ST {
public :
Value operator [](Key key) {
return get(key);
}
int size() {
return root -> size;
}
bool empty() {
return root == NULL;
}
Value get(Key key) {
if (key == NULL) exit(117);
return Get(root, key);
}
bool count(Key key) {
return get(key) != NULL;
}
void put(Key key, Value val) {
if (key == NULL) exit(117);
if (val == NULL) {
deletion(key);
return;
}
root = Put(root, key, val);
root -> color = Black;
}
void popMin() {
if (empty()) exit(118);
if (!red(root -> left) && red(root -> left))
root -> color = Red;
root = earseMin(root);
if (!empty()) root -> color = Black;
}
void popMax() {
if (empty()) return exit(118);
if (!red(root -> left) && !red(root -> right)) root -> color = Red;
root = earseMax(root);
if (!empty()) root -> color = Black;
}
void deletion(Key key) {
if (key == NULL) exit(117);
if (!count(key)) return;
if (!red(root -> left) && !red(root -> right)) root -> color = Red;
root = erase(root, key);
if (!empty()) root -> color = Black;
}
Key min() {
if (empty()) exit(116);
return Min(root) -> key;
}
Key max() {
if (empty()) exit(116);
return Max(root) -> key;
}
Key floor(Key key) {
if (key == NULL) exit(117);
if (empty()) exit(116);
Node *x = Floor(root, key);
if (x == NULL) exit(116);
else return x -> key;
}
Key ceil(Key key) {
if (key == NULL) exit(117);
if (empty()) exit(116);
Node *x = Ceil(root, key);
if (x == NULL) exit(117);
else return x -> key;
}
private :
class Node {
public :
Key key;
Value val;
Node *left, *right;
int size;
bool color;
Node(Key key, Value val, int size, bool color) {
this -> key = key;
this -> val = val;
this -> size = size;
this -> color = color;
}
};
Node *root;
bool red(Node *x) {
if (x == NULL) return false;
return x -> color == Red;
}
Node *rotateLeft(Node *h) {
Node *x = h -> left;
h -> right = x -> left;
x -> left = h;
x -> color = h -> color;
h -> color = Red;
x -> size = h -> size;
h -> size = 1 + h -> left -> size + h -> right -> size;
return x;
}
Node *rotateRight(Node *h) {
Node *x = h -> right;
h -> left = x -> right;
x -> right = h;
x -> color = h -> color;
h -> color = Red;
x -> size = h -> size;
h -> size = 1 + h -> left -> size + h -> right -> size;
return x;
}
void flipColor(Node *h) {
h -> color ^= 1;
h -> left -> color ^= 1;
h -> right -> color ^= 1;
}
Node *moveLeft(Node *h) {
flipColor(h);
if (red(h -> right -> left)) {
h -> right = rotateLeft(h -> right);
h = rotateLeft(h);
flipColor(h);
}
return h;
}
Node *moveRight(Node *h) {
flipColor(h);
if (red(h -> left -> left)) {
h = rotateRight(h);
flipColor(h);
}
return h;
}
Node *balance(Node *h) {
if (red(h -> right) && !red(h -> left))
h = rotateLeft(h);
if (red(h -> left) && red(h -> left -> left))
h = rotateRight(h);
if (red(h -> left) && red(h -> right)) flipColor(h);
h -> size = h -> left -> size + h -> right -> size + 1;
return h;
}
// Value Get(Node *x, Key key) {
// while (x != NULL) {
// if (x -> key < key) x = x -> left;
// else if (x -> key > key) x = x -> right;
// else return x -> val;
// }
// return NULL;
// }
Value Get(Node *x, Key key) {
while (x != NULL) {
if (x -> key < key) x = x -> left;
else if (x -> key > key) x = x -> right;
else return x -> val;
}
return NULL;
}
Node *Put(Node *h, Key key, Value val) {
if (h == NULL) return new Node(key, val, Red, 1);
if (key < h -> key) h -> left = Put(h -> left, key, val);
else if (key > h -> key) h -> right = Put(h -> right, key, val);
else h -> val = val;
// check it
if (red(h -> right) && !red(h -> left)) h = rotateLeft(h);
if (red(h -> left) && red(h -> left -> left)) h = rotateRight(h);
if (red(h -> left) && red(h -> right)) flipColor(h);
h -> size = h -> left -> size + h -> right -> size + 1;
}
Node *earseMin(Node *h) {
if (h -> left == NULL) return NULL;
if (!red(h -> left) && !red(h -> left -> left))
h = moveLeft(h);
h -> left = earseMin(h -> left);
return balance(h);
}
Node *earseMax(Node *h) {
if (red(h -> left)) h = rotateRight(h);
if (h -> right == NULL) return NULL;
if (!red(h -> right) && !red(h -> right)) h = moveRight(h);
h -> right = earseMax(h -> right);
return balance(h);
}
Node *erase(Node *h, Key key) {
if (key < h -> key) {
if (!red(h -> left) && !red(h -> left -> left))
h = moveLeft(h);
h -> left = erase(h -> left, key);
} else {
if (red(h -> left)) h = rotateRight(h);
if (key == h -> key && h -> right -> left == NULL)
return NULL;
if (!red(h -> right) && !red(h -> right -> left))
h = moveRight(h);
if (key == h -> key) {
Node *x = Min(h -> right);
h -> key = x -> key;
h -> key = x -> val;
h -> right = earseMin(h -> right);
} else h -> right = erase(h -> right, key);
}
}
Node *Min(Node *x) {
if (x -> left == NULL) return x;
else return Min(x -> left);
}
Node *Max(Node *x) {
if (x -> right == NULL) return x;
else return Max(x -> right);
}
Node *Floor(Node x, Key key) {
if (x == NULL) return NULL;
if (key == x -> key) return x;
if (key < x -> key) return Floor(x -> left, key);
Node *t = Floor(x -> right, key);
if (t != NULL) return t;
else return x;
}
Node *Ceil(Node *x, Key key) {
if (x == NULL) return NULL;
if (x -> key == key) return x;
if (x -> key < key) return Ceil(x -> right, key);
Node *t = Ceil(x -> left, key);
if (t != NULL) return t;
else return x;
}
};
int main(int argc, char *argv[]) {
return 0;
}pair
#include <stdio.h>
#include <stdlib.h>
template<typename Item1, typename Item2>
class pair {
public :
// 测试用例
static void main() {
pair<int, int> p = {2, 3};
p.print();
}
// 构造函数们
pair()
{ first = second = 0; }
pair(const Item1 &a, const Item2 &b) {
first = a;
second = b;
}
pair(const pair<Item1, Item2> &other) {
first = other.first;
second = other.second;
}
// 构造一个 pair
static pair<Item1, Item2> make_pair(Item1 x, Item2 y)
{ return {x, y}; }
// 打印 pair
void print()
{ printf("first: %d,second: %d", first, second); }
Item1 first;
Item2 second;
};
int main() {
pair<int, int>::main();
return 0;
}Stack
#include <stdio.h>
#include <stdlib.h>
typedef unsigned long long size_type;
template<typename Item>
class Stack
{
public :
Item top()
{
return first -> data;
}
void push(const Item &x)
{
if (first == NULL)
{
first = new Node;
first -> data = x;
}
else
{
Node *New = new Node;
New -> data = x;
New -> next = first;
first = New;
}
}
Item pop()
{
if (first == NULL)
{ return 0; exit(116); }
else
{
Node *Old = first -> next;
Item value = first -> data;
delete first;
first = Old;
return value;
}
}
bool empty()
{ return length == 0; }
size_type size()
{ return length; }
void clear()
{ while (!empty()) pop(); }
private :
class Node
{
public :
Item data;
Node *next;
};
Node *first;
size_type length;
};
signed main()
{
Stack<int> s;
s.push(1);
s.push(2);
s.pop();
printf("%d", s.top());
return 0;
}Queue
#include <stdio.h>
#include <stdlib.h>
template<typename Item>
class Queue
{
public :
Item front()
{
return first -> data;
}
void push(const Item &x)
{
if (first == NULL)
{
first = new Node;
first -> data = x;
last = first;
}
else
{
Node *New = new Node;
New -> data = x;
last -> next = New;
last = New;
}
}
Item pop()
{
if (first == NULL)
{ return 0; exit(117); }
else
{
Node *New = first -> next;
Item value = first -> data;
delete first;
first = New;
return value;
}
}
private :
class Node
{
public :
Item data;
Node *next;
};
Node *first;
Node *last;
};
signed main() {
Queue<int> Q;
Q.push(1);
Q.push(2);
printf("%d", Q.pop());
return 0;
}