算法模板
默默可书虫
这个作者很懒,什么都没留下…
展开
-
<模板>中缀转后缀
注:中缀表达式转后缀表达式,要求输入的中缀表达式不能带正负号,括号只有’('和‘)’,且不能有多余的括号,运算符只有加减乘除。若想实现更一般的功能,需要再完善一下代码。 #include<iostream> #include<string> #include<stack> using namespace std; stack<char> stk; //栈中存储的只有运算符号 bool justic(char a, char b) //判断a的优先级是不是原创 2021-05-26 19:17:14 · 178 阅读 · 0 评论 -
<模板>二叉Huffman树
以叶节点的权值为1,2,3,4,5,求解二叉哈夫曼树的问题。直接计算了结果没有构建出哈夫曼树。 计算的问题是:求最小的∑wi∗li\sum {w_i*l_i}∑wi∗li,其中w为叶节点,L为叶节点到根的深度。 #include<iostream> #include<queue> using namespace std; struct rec //定义了结构体,方便重载小于号 { int v; }; rec exm; priority_queue<rec&原创 2021-05-19 17:21:02 · 161 阅读 · 0 评论 -
<模板>计数排序
计数排序是一种可以将时间复杂度降低为O(n)的非比较排序,因为它不需要两两元素进行比较所以比较排序的上界O(nlgn)并不适用于它,不过它有较多的限制条件: 1、输入的n个元素必须是整数。 2、数据的符号应保持一致。 3、数据的范围不能太大,即数据的最大值k不能过大。 4、数据之间的跨越幅度不宜过大,否则会浪费较多空间。 代码如下: #include<iostream> using namespace std; const int k = 30; //k是要排序数组中的最大值 int B[k原创 2021-05-06 17:07:07 · 108 阅读 · 0 评论 -
<模板>大根堆
#include<iostream> using namespace std; const int SIZE = 110; int heap[SIZE], n; //heap 从下标1开始存储的,n是数组中已有的元素 void up(int p) { while (p > 1) { if (heap[p] > heap[p / 2]) { swap(heap[p], heap[p / 2]); p = p / 2; } else { break;原创 2021-05-03 13:54:21 · 49 阅读 · 0 评论 -
<模板>快速排序
#include<iostream> #include<algorithm> using namespace std; void quick_sort(int q[], int l, int r) { if (l >= r) return; int i = l - 1, j = r + 1, t = q[l + r>>1]; while (i < j) { do i++; while (q[i] < t); do j--; whil原创 2021-04-30 10:19:05 · 62 阅读 · 0 评论 -
<模板>正整数转32位二进制字符串
把一个正整数转为32位二进制字符串,其中数据范围是0<=x<2310<=x<2^{31}0<=x<231 #include<iostream> using namespace std; void itobs(int i, char* c) { int j = 0; while (j < 32) { if ((i & (1 << (31 - j))) != 0) c[j] = '1'; else c[j] = '0';原创 2021-04-28 20:02:18 · 228 阅读 · 0 评论 -
<模板>Trie字典树
字典树,一种存储字符串的结构,可以快速的找到某一字符串是否在字典树中。 int trie[SIZE][26],tot=1; //假设输入为26个小写字母 void insert(char* str) //插入数据 { int len=strlen(str),p=1; for(int k=0;k<len;k++) { int ch=str[k]-'a'; if(trie[p][ch]==0) trie[p][ch]=++tot; p=trie[p][ch]; } endk[原创 2021-04-26 16:03:52 · 65 阅读 · 0 评论 -
<模板>单调栈
#include<iostream> using namespace std; //int a[100]; //原始数组,数组最后一个元素要设为一个极小值 const int N = 8; int q[100]; //单调递增的栈 int res[N]; //结果数组,第i个存的数表示a[i]左边第一个小于它的数 int n = N; //a数组的长度 void calc_stk(int *a) { int p = 0; for (int i = 1; i <= n; i++原创 2021-04-23 20:02:54 · 79 阅读 · 0 评论 -
<模板>高精度减法
计算两正整数的减法,结果可能小于0.所谓高精度就是指可以计算超出计算机量程范围的数的减法。 #include<iostream> #include<vector> #include<string> using namespace std; vector<int> sub(vector<int>& A, vector<int>& B) //倒着的高精度减法 { vector<int> c; for (i原创 2021-04-20 19:42:55 · 68 阅读 · 0 评论 -
<模板>高精度加法
#include<iostream> #include<vector> using namespace std; vector<int> add(vector<int>& A, vector<int>& B) { //其实就是模拟了竖式加法的操作过程 if (A.size() < B.size()) return add(B, A); //在内部调用居然只用传参就可以了 int t = 0; vector<int原创 2021-04-20 19:36:26 · 50 阅读 · 0 评论 -
kmp匹配模板
#include<iostream> using namespace std; char a[1000010]; char b[1000010]; //两个字符串数组,待匹配的两个字符串 int next1[1000010] , n,m; //n是字符串a的长度,m是字符串b的长度 int f[1000010]; //计算字符串的next数组 void calc_next() { next1[1] = 0; for (int i = 2, j = 0; i <= n;原创 2021-04-20 19:33:22 · 56 阅读 · 0 评论 -
字符串hash模板
总是换电脑,我决定把这些通用的模板记在这里。 #include<iostream> using namespace std; char s[1000010]; //存贮要计算哈希值的字符串 此例子中字符串只包含小写英文字母 unsigned long long h[1000010], p[1000010]; //h存哈希值的计算结果,p存那个质数的幂,p一般取131 此类型unsigned long long 计算时不用处理溢出 //产生溢出时,相当于自动对2^64取模原创 2021-04-20 19:30:14 · 183 阅读 · 0 评论