Browse Source

上传文件至 'Structure'

第一次上传数据结构
ZZWX 1 year ago
parent
commit
5699323502

+ 46 - 0
Structure/自定义二叉堆模板.cpp

@@ -0,0 +1,46 @@
+class tph_inc {
+private:
+	template<typename _T>
+	static void swap(_T& a, _T& b) {
+		_T t = a;
+		a = b;
+		b = t;
+	}
+
+	template<typename _T, typename _U>
+	static void up(int at, int& siz, _T* heap, bool(*comp)(_U, _U)) {
+		while (at > 1 && !((*comp)(heap[at >> 1], heap[at]))) {
+			swap(heap[at >> 1], heap[at]);
+			at >>= 1;
+		}
+	}
+
+	template<typename _T, typename _U>
+	static void down(int at, int& siz, _T* heap, bool(*comp)(_U, _U)) {
+		while ((at << 1) <= siz) {
+			int mson = at << 1;
+			if (mson + 1 <= siz && (*comp)(heap[mson + 1], heap[mson]))
+				mson++;
+			if ((*comp)(heap[at], heap[mson]))
+				break;
+			swap(heap[at], heap[mson]);
+			at = mson;
+		}
+	}
+public:
+	template<typename _T, typename _U>
+	static void ins(_T x, int& siz, _T* heap, bool(*comp)(_U, _U)) {
+		heap[++siz] = x;
+		up(siz, siz, heap, comp);
+	}
+
+	template<typename _T, typename _U>
+	static void del(int at, int& siz, _T* heap, bool(*comp)(_U, _U)) {
+		swap(heap[at], heap[siz]);
+		heap[siz--] = 0;
+		if ((!((*comp)(heap[at >> 1], heap[at]))))
+			up(at, siz, heap, comp);
+		else
+			down(at, siz, heap, comp);
+	}
+};

+ 86 - 0
Structure/自定义单调栈模板.cpp

@@ -0,0 +1,86 @@
+#include <stack>
+#include <vector>class monstk_std {
+public:
+    //std::stack version
+    //compare function : from the bottom to the top
+    template<typename _T, typename _U>
+    static std::vector<_T> ins(_T x, std::stack<_T>& stk, bool (*comp)(_U, _U)) {
+        std::vector<_T> vec;
+        while ((!stk.empty()) && (!((*comp)(stk.top(), x))))
+            vec.push_back(stk.top()),
+            stk.pop();
+        stk.push(x);
+        return vec;
+    }
+    //compare function : from the bottom to the top
+    template<typename _T, typename _U>
+    static void ins(std::vector<_T>& vec, _T x, std::stack<_T>& stk, bool (*comp)(_U, _U)) {
+        while ((!stk.empty()) && (!((*comp)(stk.top(), x))))
+            vec.push_back(stk.top()),
+            stk.pop();
+        stk.push(x);
+    }
+    template<typename _T>
+    static void ext(std::vector<_T>& vec, std::stack<_T>& stk) {
+        while (!stk.empty()) {
+            vec.push_back(stk.top());
+            stk.pop();
+        }
+    }
+    template<typename _T>
+    static std::vector<_T> ext(std::stack<_T>& stk) {
+        std::vector<_T> vec;
+        while (!stk.empty())
+            vec.push_back(stk.top()),
+            stk.pop();
+        return vec;
+    }
+};
+
+
+
+//-----------------------------------------------------------------------------//
+class monstk_pnt {
+    //pointer(list) version
+
+
+
+    //compare function : from the bottom to the top
+    template<typename _T, typename _U>
+    static void ins(std::vector<_T>& vec, _T x, int& siz, _T* stk, bool (*comp)(_U, _U)) {
+        while ((siz > 0) && (!((*comp)(stk[siz], x)))) {
+            vec.push_back(stk[siz]);
+            siz--;
+        }
+        siz++;
+        stk[siz] = x;
+    }
+    //compare function : from the bottom to the top
+    template<typename _T, typename _U>
+    staticstd::vector<_T> ins(_T x, int& siz, _T* stk, bool (*comp)(_U, _U)) {
+        std::vector<_T> vec;
+        while ((siz > 0) && (!((*comp)(stk[siz], x)))) {
+            vec.push_back(stk[siz]);
+            siz--;
+        }
+        siz++;
+        stk[siz] = x;
+        return vec;
+    }
+    template<typename _T>
+    static std::vector<_T> ext(int& siz, _T* stk) {
+        std::vector<_T> vec;
+        while (siz > 0) {
+            vec.push_back(stk[siz]);
+            siz--;
+        }
+        return vec;
+    }
+    template<typename _T>
+    static void ext(std::vector<_T>& vec, int& siz, _T* stk) {
+        while (siz > 0) {
+            vec.push_back(stk[siz]);
+            siz--;
+        }
+    }
+};

+ 36 - 0
Structure/自定义单调队列模板.cpp

@@ -0,0 +1,36 @@
+#include <deque>
+#include <vector>
+class monque_inc{
+	//compare function:from head to rear
+	template<typename _T, typename _U>
+	static std::vector<_T> ins(_T x, std::deque<_T>&que, bool (*comp)(_U, _U)) {
+		std::vector<_T> vec;
+		while (!((*comp)(que.back(), x)))
+			vec.push_back(que.back()),
+			que.pop_back();
+		que.push_back(x);
+		return vec;
+	}
+	//compare function:from head to rear
+	template<typename _T, typename _U>
+	static void ins(_T x, std::deque<_T>& que, bool (*comp)(_U, _U), std::vector<_T>& vec){
+		while (!((*comp)(que.back(), x)))
+			vec.push_back(que.back()),
+			que.pop_back();
+		que.push_back(x);
+	}
+	template<typename _T>
+	static std::vector<_T> fiton(int siz, std::deque<_T>& que) {
+		std::vector<_T> vec;
+		while (siz > que.size())
+			vec.push_back(que.front()),
+			que.pop_front();
+		return vec;
+	}
+	template<typename _T>
+	static void fiton(int siz, std::deque<_T>& que, std::vector<_T>& vec) {
+		while (siz > que.size())
+			vec.push_back(que.front()),
+			que.pop_front();
+	}
+};

+ 127 - 0
Structure/自定义字典树模板.cpp

@@ -0,0 +1,127 @@
+#include <map>
+#include <string>
+class Trie_std {
+private:
+	struct diag {
+		std::map<char, diag*> son;
+		diag* father;
+		char ch;
+		int endcnt;
+		diag()
+			: son(), ch(0), endcnt(0), father(nullptr) {}
+		diag(const std::map<char, diag*>& son, char ch, int endcnt)
+			: son(son), ch(ch), endcnt(endcnt), father(nullptr) {}
+	} *root;
+public:
+	Trie_std() {
+		root = new(diag);
+	}
+	void insert(std::string x) {
+		int siz = x.size();
+		diag* now = root;
+		for (int i = 0; i < siz; i++) {
+			if ((now->son.find(x[i])) == (now->son.end()))
+				now->son[x[i]] = new(diag),
+				now->son[x[i]]->father = now,
+				now->son[x[i]]->ch = x[i];
+			now = now->son[x[i]];
+		}
+		now->endcnt++;
+	}
+	bool find(std::string x) {
+		int siz = x.size();
+		diag* now = root;
+		for (int i = 0; i < siz; i++) {
+			if (now->son.find(x[i]) == now->son.end())
+				return false;
+			now = now->son[x[i]];
+		}
+		if (now->endcnt)
+			return true;
+		else
+			return false;
+	}
+	bool erase(std::string x) {
+		int siz = x.size();
+		diag* now = root;
+		for (int i = 0; i < siz; i++) {
+			if (now->son.find(x[i]) == now->son.end())
+				return false;
+			now = now->son[x[i]];
+		}
+		diag* t;
+		now->endcnt--;
+		while (siz--) {
+			t = now->father;
+			if (!(now->son.size())) {
+				t->son.erase(now->ch);
+				delete now;
+			}
+			else return true;
+			now = t;
+		}
+		return true;
+	}
+};
+
+
+class Trie_lst {
+private:
+	struct diag {
+		std::map<char, diag*> son;
+		diag* father;
+		char ch;
+		int endcnt;
+		diag()
+			: son(), ch(0), endcnt(0), father(nullptr) {}
+		diag(const std::map<char, diag*>& son, char ch, int endcnt)
+			: son(son), ch(ch), endcnt(endcnt), father(nullptr) {}
+	} *root;
+public:
+	Trie_lst() {
+		root = new(diag);
+	}
+	void insert(char* x, int siz) {
+		diag* now = root;
+		for (int i = 0; i < siz; i++) {
+			if ((now->son.find(x[i])) == (now->son.end()))
+				now->son[x[i]] = new(diag),
+				now->son[x[i]]->father = now,
+				now->son[x[i]]->ch = x[i];
+			now = now->son[x[i]];
+		}
+		now->endcnt++;
+	}
+	bool find(char* x, int siz) {
+		diag* now = root;
+		for (int i = 0; i < siz; i++) {
+			if (now->son.find(x[i]) == now->son.end())
+				return false;
+			now = now->son[x[i]];
+		}
+		if (now->endcnt)
+			return true;
+		else
+			return false;
+	}
+	bool erase(char* x, int siz) {
+		diag* now = root;
+		for (int i = 0; i < siz; i++) {
+			if (now->son.find(x[i]) == now->son.end())
+				return false;
+			now = now->son[x[i]];
+		}
+		diag* t;
+		now->endcnt--;
+		while (siz--) {
+			t = now->father;
+			if (!(now->son.size())) {
+				t->son.erase(now->ch);
+				delete now;
+			}
+			else return true;
+			now = t;
+		}
+		return true;
+	}
+};

+ 11 - 0
Structure/自定义并查集模板.cpp

@@ -0,0 +1,11 @@
+int f[/*size*/];
+int find(int x) {
+	return (f[x] == x) ? x : (f[x] = find(f[x]));
+}
+void merge(int x, int y) {
+	int fx = find(x), fy = find(y);
+	f[fx] = fy;
+}
+bool query(int x, int y) {
+	return find(x) == find(y);
+}