#include<iostream> usingnamespacestd; #define MAX 500000 #define SENTIVITY 1000000000
int L[MAX/2+1],R[MAX/2+1]; int cnt;
voidmerge(int A[], int n, int left, int mid, int right){ int n1 = mid - left; int n2 = right - mid; for (int i = 0; i < n1; ++i) L[i] = A[left + i]; for (int j = 0; j < n2; ++j) R[j] = A[mid + j];//开辟两个独立数组,存储数据 L[n1] = R[n2] = SENTIVITY; int i = 0, j = 0; for (int k = left; k < right; ++k){ cnt++; if (L[i] <= R[j]){ A[k] = L[i++]; }else{ A[k] = R[j++]; }//数据按照大小依次插入 } }
voidmergesort(int A[], int n, int left, int right){ if (left+1 < right){ int mid = (left + right)/2; mergesort(A, n, left, mid);//前半部分排序 mergesort(A, n, mid, right);//后半部分排序 merge(A, n, left, mid, right);//融合 } }
intmain(){ int A[MAX], n, i; cnt = 0; scanf("%d",&n); for(i = 0; i < n; ++i){ scanf("%d",&A[i]); } mergesort(A, n, 0, n); for (i = 0; i < n; ++i){ printf("%d",A[i]); printf(" "); } cout << endl; printf("%d", cnt); return0; }
快速排序:时间复杂度$O(n\log n)$
1 2 3 4 5 6 7 8 9
//分割算法
//快排算法 quickSort(A,p,r) if p<r q=partition(A,p,r) quicksort(A,p,q-1) quicksort(A,q+1,r)
voidsetDepth(int u, int p){ D[u] = p;//给u结点设置为深度p(相当于设置一个起始结点); if (T[u].r != NIL) setDepth(T(u).r, p);//右兄弟:结点深度一致; if (T[u].l != NIL) setDepth(T(u).l, p+1);//左子:结点深度加一; }
二叉树的表达
结点用结构体表达
1 2 3
structNode{ int parent, left, right; };
二叉树的输入
1 2 3 4 5 6 7
for (int i = 0; i < n; ++i){ //n表示所有结点的总数目 scanf("%d %d %d", &v, &l, &r); T[v].left = l;//设置子结点 T[v].right = r; if (l != NIL) T[l].parent = v;//设置父结点 if (r != NIL) T[r].parent = v; }
结点的深度利用递归方法求解(自顶向下)
1 2 3 4 5 6
voidsetDepth(int u, int p){ if (u == NIL) return;//对于异常值,表示结束 D[u] = p;//给u结点设置为深度p(相当于设置一个起始结点); if (T[u].r != NIL) setDepth(T(u).right, p+1);//右子:结点深度加一; if (T[u].l != NIL) setDepth(T(u).left, p+1);//左子:结点深度加一; }
二叉树的高度递归法求解(自底向上)
1 2 3 4 5 6 7 8
intsetHeight(int u){ int h1 = 0, h2 = 0;//初始化变量 if (T[u].left != NIL) h1 = setHeight(T[u].left) + 1;//左结点的高度基础上加一 if (T[u].left != NIL) h2 = setHeight(T[u].right) + 1;//右结点的高度基础上加一 returnmax(h1, h2); }
树的遍历
前序遍历:根节点、左子树、右子树的顺序输出
1 2 3 4 5 6
voidpreParse(int u){ if (u == NIL) return; printf("%d", u); preParse(T[u].left); preParse(T[u].right); }
中序遍历:左子树、根节点、右子树的顺序输出
1 2 3 4 5 6
voidinorder(int u){ if (u == NIL) return; inorder(T[u].left); printf("%d", u); inorder(T[u].right); }
后序遍历:左子树、右子树、根节点的顺序输出
1 2 3 4 5 6
voidpostorder(int u){ if (u == NIL) return; postorder(T[u].left); postorder(T[u].right); printf("%d", u); }
#include<iostream> usingnamespacestd; #define MAX 2000000
int H, A[MAX+1];//H是数组长度,A代表数组。(对于未知长度数组可以使用vector)
voidmaxHeapify(int i){ int l, r, largest; l = 2 * i;//左结点 r = 2 * i + 1;//右结点 //找出左右结点中较大的一个 if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (l <= H && A[r] > A[largest]) largest = r; //将当前结点与较大的一个交换,同时循环进行; if (largest != i){ swap(A[i], A[largest]);//交换两个函数的值 maxHeapify(largest); } }
intmain(){ scanf("%d", &H); for (int i = 1; i <= H; ++i) scanf("%d", &A[i]); for (int i = H/2; i >= i; --i) maxHeapify(i);//以2/H为起点,1位终点,自底向上交换 for (int i = 1; i <= H; ++i) printf("%d", A[i]); printf("\n"); return0; }
#include<iostream> #include<string> #include<algorithm> usingnamespacestd; staticconstint N = 1000;
intlcs(string X, string Y){ int c[N+1][N+1];//开辟二维数组计算已有计算结构 int m = X.size(); int n = Y.size();//计算给定序列的长度 int maxl = 0;//初始化结果 X = ' ' + X; Y = ' ' + Y;//为处理方便,在0位置上插入一个空位 for (int i = 0; i <= m; ++i) c[i][0] = 0; for (int j = 0; j <= n; ++j) c[0][j] = 0; for (int i = 0; i <= n; ++i){ for (int j = 0; j <= m; ++j){ if (X[i] == Y[j]) c[i][j] = c[i-1][j-1] + 1; else c[i][j] = max(c[i][j-1], c[i-1][j]); maxl = max(maxl, c[i][j]);//更新答案 } } return maxl; }
intmain(){ int n; scanf("%d", &n); string X, Y; for (int i = 0; i < n; ++i){ cin >> X >> Y; printf("%d", lcs(X,Y)); } return0; }
intmain(){ int n, p[N+1], m[N+1][N+1]; scanf("%d", &n); for (int i = 1; i <= n; ++i){ scanf("%d%d", &p[i-1], &p[i]);//存入行、列数目 } //m[i][j]表示从第i个矩阵乘至第j个矩阵的成本,那么在j小于i的情形下的结果不予j考虑; for (int i = 0; i <= n; ++i) m[i][i] = 0; //对于i,j相等的情况,实际上并没有进行计算 for (int l = 2; l <= n; ++l){//l表示用于计算的矩阵的个数,从而控制i,j所在的范围 for (int i = 1; i <= n - l + 1; ++i){ int j = i + l - 1; m[i][j] = (1 << 21); for (int k = i; k <= j - 1; ++k){ m[i][j] = min(m[i][j],m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]); } } } printf("%d", m[1][n]); return0; }
voidbfs(int s){ queue<int> q; q.push(s); for (int i = 0; i < n; ++i) d[i] = INFTY; d[s] = 0; int u; while(!q.empty()){ u = q.front(); q.pop(); for (int v = 0; v < n; ++v){ if (M[u][v] == 0) continue; if (d[v] != INFTY) continue; d[v] = d[u] + 1; q.push(v); } } }
intmain(){ scanf("%d%d", &n, &m); //初始化 for (int i = 0; i < n; ++i){ for (int j = 0; j < n; ++j){ M[i][j] = 0; } } //存入图结构 for (int i = 0; i < m; ++i){ int u, v; scanf("%d%d", &u, &v); u--; v--; M[u][v] = 1; }
scanf("%d", &q); for (int i = 0; i < q; ++i){ int u, v; scanf("%d%d", &u, &v); u--; v--; bfs(u); if(d[v] != INFTY) cout << "yes" << endl; elsecout << "no" << endl; } return0; }
voiddfs(int r, int c){ stack<int> S; S.push(r); color[r] = c; while(!S.empty()){ int u = S.top(); s.pop(); for (int i = 0; i < G[u].size(); ++i){ int v = G[u][i]; //如果还没有上色,就上色并以此作为结点继续上色 if (color[v] == NIL){ color[v] = c; S.push(v); } } } }
voidassignColor(){ int id = 1; for (int i = 0; i < n; ++i) color[i] = NIL; for (int u = 0; u < n; ++i){ if (color[u] == NIL) dfs(u, id++); //以u为起点展开深度优先搜索,并存储颜色,这样可以保持同一棵连通图 } }
intmain(){ int s, t, m, q; scanf("%d%d", n, m); //将关系存入邻接表 for (int i = 0; i < m; ++i){ scanf("%d%d", &s, &t); G[s].push_back(t); G[t].push_back(s); } assignColor(); scanf("%d", &q); for (int i = 0; i < q; ++i){ scanf("%d%d", &s, &t); if (color[s] == color[t]) cout << "yes" << endl; elsecout << "no" << endl; } return0; }
This is copyright.