c/c++
MiniSheep_CS
编程者如上帝,可以创造万物。
展开
-
二叉堆的实现和详解(优先队列的基础)
二叉堆的基本内容: 由于堆是一颗被完全填满的二叉树,所以最后一层是从左到右一次填入的,所以可以不必要链表表示(不连续),可以用数组表示比较节省开销(vector)。在堆操作中,两个比较重要和新的内容是上滤和下滤,个人总结了一下,插入新的结点的时候用上滤,删除最小结点的时候用下滤。还有一个值得注意的概念,它叫空穴,空穴顾名思义是空的,比如删除了根结点的元素,那么现在根结点即为空穴。好接下来我们原创 2015-01-11 22:29:54 · 734 阅读 · 0 评论 -
bellman-ford算法(判断是否存在负环)
Bellman-ford算法的一个重要应用是判负环。在迭代n-1次后如果还可以进行松弛操作,说明一定存在负环。如果采用队列实现,那么当某个结点入队了n次时可以判断出存在负环,代码如下: #include #include #include #include #include #include using namespace std; const int maxn = 1001; struct E原创 2015-02-04 19:57:41 · 4527 阅读 · 0 评论 -
c++类模版和运算符重载的运用
最近在看数据结构算法c++描述,很久没写c++了,所以就当回忆下,所以以下代码就当练手,输出的是工资的最大值人的姓名和它的工资: #include #include #include using namespace std; template //模版 const Compareable & findMax(const vector & arr) //Compareable可以表示任意类原创 2014-12-13 17:46:00 · 484 阅读 · 0 评论 -
map的例子
题意:输入一堆字符串,然后可以任意替换其中的一个字母,给定一个最低底线(例如输入15)表示必须要有15个以上只有一个字母不同的字符串组成的数组(这里用map>,第一个表示当前遍历到的字符串,vector里面存的是和遍历到的字符串只差一个字母的那些字符串),然后要求你编程实现,下面为代码: #include #include #include #include #include #includ原创 2015-01-01 12:48:22 · 479 阅读 · 0 评论 -
STL之priority_queue的使用
priority_queue说明: 优先队列的实现就不说了,运用的是堆的知识,前几篇博客已经有提到。现在讲讲如何在比赛的时候直接用STL里面的队列头文件中的priority_queue来帮我们解题。下文有两种使用方法,一种是运用#include中的greater和less来帮我们实现数据的排序。还有一种是自定义,也就是自己写排序方法让priority_queue来根据我们排序方法来排序。 代码原创 2015-02-13 18:02:12 · 495 阅读 · 0 评论 -
Kruskal最小生成树算法
#include #include #include using namespace std; const int maxn = 1001; int fa[maxn]; int N,M; struct edge{ int x,y,w; edge(int x = 0,int y = 0,int w = 0):x(x),y(y),w(w) {} }e[maxn]; bool cmp(e原创 2015-02-09 20:52:14 · 493 阅读 · 0 评论 -
求解无向图的割点和桥
无向图的割点和桥 割点和桥的概念: 对于无向图G,如果删除某个点u后,连通分量数目增加(本来为一个连通集,现在变为多个),称u为图的割点。对于连通图,割点就是删除之后不再连通的点。当然了,桥也同理,只不过桥是边,也称为割边,如果删掉(u,v)之后连通分量数目增加,那么我们称(u,v)这条边为桥(割边)。 求割点和桥的方法: ①很容易想到的,暴力每个结点,然后用DFS判断连通分量是否增加,时原创 2015-02-11 20:19:45 · 809 阅读 · 0 评论 -
hdu1166 敌兵布阵(线段树入门题)
#include using namespace std; const int maxn = 50001; struct tree { int left,right; int sum; }node; tree t[maxn * 3]; //结点数 int a[maxn]; //元素的值 void build(int num,int left,int right) //num表示序号原创 2015-02-15 17:31:35 · 482 阅读 · 0 评论 -
哈希表的C++实现
#include using namespace std; typedef int KeyType; //设关键字域为整形,需要修改类型时,只需修改这里就可以 const int NULLKEY=0; //NULLKEY表示该位置无值 int c=0; //用来统计冲突次数 struct Elemtype //转载 2015-04-06 15:42:02 · 450 阅读 · 0 评论 -
左式堆的实现与详解
左式堆的性质: 像二叉堆那样既有结构性质,又有堆序性质。事实上,和所有使用的堆一样,左式堆具有相同的堆序性质,该性质我们已经看到过。不仅如此,左式堆也是二叉树,左式堆和二叉堆唯一的区别是:左式堆不是理想平衡的,而且事实上是趋于非常不平衡的。 零路径长:(null path length) 假设有个结点x,npl(x)定义为x到一个不具有两个儿子的结点的最短路径的长,因此,具有0个或1个儿子的原创 2015-01-15 12:22:04 · 905 阅读 · 0 评论 -
图论之无权最短路径
无权最短路径顾名思义是边没有权值,所以我们可以把所有的边都赋值为1,求最短路径可以采用广度优先搜索(BFS),该方法按层处理顶点,距开始点最近的那些顶点首先被求值,而最远的那些顶点最后被求值。这很像对树的层序遍历。首先先把不通的路径置为无穷大(我这里置为-1),可以通的路置为1,然后运用广度优先搜索(使用的是队列实现),先取一个点作为始发点,这里取v3,然后把v3加入队列,然后一次寻找可以通的路,原创 2015-02-03 12:22:30 · 660 阅读 · 0 评论 -
Avl树(二叉平衡树)
#include #include using namespace std; static int arr[] = { 3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 }; #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) ) template class AVLTreeNode{原创 2014-12-29 19:53:29 · 585 阅读 · 0 评论 -
数据结构之查找二叉树--BST
#include using namespace std; template class BinarySearchTree { public: BinarySearchTree(); BinarySearchTree(const BinarySearchTree& rhs); //copy constructor ~BinarySearchTree(); //destru原创 2014-12-24 12:44:36 · 471 阅读 · 0 评论 -
计算后缀表达式的值
#include #include #include #include #include using namespace std; double evalPostFix() { stack s; string token; double a,b,result; cin>>token; while(token[0] != '=') {原创 2014-12-18 13:11:21 · 688 阅读 · 0 评论 -
二叉树的遍历
#include #include #include using namespace std; //二叉树结点 typedef struct BiTNode{ //数据 char data; //左右孩子指针 struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; //按先序序列创建二叉树 int CreateBiTree(BiTree &T原创 2014-12-22 18:37:52 · 487 阅读 · 0 评论 -
STL list容器实现集合的交和并
集合的交:#include #include #include using namespace std; template list intersection(const list &L1,const list &L2) { list intersect; typename list::const_iterator iterL1 = L1.begin(); typename原创 2014-12-21 20:16:32 · 490 阅读 · 0 评论 -
将中缀表达式转换成后缀表达式
#include #include #include #include #include using namespace std; void inToPostfix() { stack s; char token; cin>>token; while(token != '=') { if(token >= 'a' && token <= '原创 2014-12-18 14:14:41 · 465 阅读 · 0 评论 -
vector容器内部实现机理(源代码和测试代码)
//Vector.h,个人实现的头文件,源文件引用时用#include "Vector" #ifndef VECTOR_INCLUDED #define VECTOR_INCLUDED template class Vector { public: explicit Vector(int initSize = 0):theSize(initSize),theCapacity(initSiz原创 2014-12-16 15:07:09 · 1143 阅读 · 0 评论 -
图论之拓扑排序
引例: 首先先举一个拓扑排序比较贴近实际的例子吧。比如我想修操作系统这门课,假设学校要求必须先修数据结构和C语言,我必须满足修过数据结构和c语言才可以修操作系统这门课。但是有一个很奇怪的问题就是如果反过来如果要求学C语言和数据结构之前要学操作系统,那么就会出现问题,所以拓扑排序的充要条件就是不能出现回路,即出发点和终点都是自身的情况。 实现: 寻找一个尚未分配拓扑编号的入度为0的顶点,如原创 2015-01-31 21:46:51 · 451 阅读 · 0 评论 -
POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 60927 Accepted: 23381 Description Every time it rains on Farmer John's fields, a pond form原创 2015-05-24 18:37:10 · 336 阅读 · 0 评论