![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构
文章平均质量分 51
skajre
这个作者很懒,什么都没留下…
展开
-
kruskal算法+并查集 输出每一条路径
#include#includeusing namespace std;#define MAX 150int m,n,u[MAX],v[MAX],w[MAX],p[MAX],r[MAX];int find(int x){return p[x]==x?x:p[x]=find(p[x]);}int cmp(const int i,const int j) {return w[i]<w[j]原创 2016-05-08 20:15:22 · 473 阅读 · 0 评论 -
分块查找
#include#includeusing namespace std;#define MAX 10typedef int keytype;typedef struct{ keytype key; int link;}Idxtype;int Idxsearch(Idxtype a[],int b,keytype s[],keytype key,int n){原创 2016-05-15 09:54:38 · 314 阅读 · 0 评论 -
二分查找
#include#includeusing namespace std;int BinartSearch(int a[],int n,int k){ int low=1; int high=n; int mid; while(low<=high) { mid=(low+high)/2; if(a[mid]==k) re原创 2016-05-15 08:38:36 · 213 阅读 · 0 评论 -
归并排序
时间复杂度nlogn,空间复杂度n,归并排序是稳定的排序方法。//归并排序#include#includeusing namespace std;void Merge(int a[],int b[],int i,int m,int j){ int x=i; int y=m+1; int k=i; while(x<=m&&y<=j) {原创 2016-05-14 21:45:51 · 197 阅读 · 0 评论 -
堆排序非递归算法
时间复杂度nlogn,空间复杂度O(1),是不稳定的排序方法。//堆排序非递归算法#include#includeusing namespace std;void AdjustHeap(int a[],int n,int k){ int i=k; int j=2*i; a[0]=a[i]; while(j<=n) { if(ja[j原创 2016-05-14 18:08:43 · 505 阅读 · 1 评论 -
堆排序递归算法
//堆排序递归算法#include#includeusing namespace std;void AdjustHeap(int a[],int n,int k){ int i=k; int j=2*i; a[0]=a[i]; if(i<=n/2) { if(ja[j]) j=j+1; if(a[j]>a[0])原创 2016-05-14 18:34:56 · 500 阅读 · 0 评论 -
树形选择排序
时间复杂度nlogn, 空间复杂度n,这是不稳定排序方法//树形选择排序#include#includeusing namespace std;int q[100],l[100];void creInitTree(int l[],int n){ int m=2*n-1; for(int i=n;i<=m;i++) q[i]=l[i-n+1];原创 2016-05-14 16:30:46 · 1025 阅读 · 0 评论 -
选择排序
时间复杂度O(n^2),空间复杂度O(1),这是不稳定的排序方法。//选择排序#include#includeusing namespace std;void SelectSort(int a[],int n){ for(int i=1;i<n;i++) { int k=i; for(int j=i+1;j<=n;j++)原创 2016-05-14 16:02:52 · 171 阅读 · 0 评论 -
快速排序
//快速排序#include#includeusing namespace std;void quicksort(int a[],int low,int high){ if(low<high) { int i=low,j=high; a[0]=a[i]; while(i<j) { wh原创 2016-05-14 15:53:39 · 214 阅读 · 0 评论 -
表插入排序
时间复杂度O(n^2),空间复杂度O(n),这是稳定的排序方法//表插入排序#include#includeusing namespace std;#define Size 10#define MAX 1000typedef int Elemtype;typedef struct{ Elemtype key; int next;}SLNode;typede原创 2016-05-14 14:59:36 · 527 阅读 · 0 评论 -
折半插入查找
先利用二分查找找到a[[i]在1~i中插入的位置,再移位,插入。空间复杂度O(1),时间复杂度O(n^2),该算法比插入算法好些,属于稳定的排序方法。//折半插入排序#include#includeusing namespace std;#define MAX 100void BinartInsertSort(int a[MAX],int n){ int low,high,原创 2016-05-14 13:34:53 · 621 阅读 · 0 评论 -
图论 邻接矩阵
//测试数据 5 5 0/*1 21 33 42 42 5*///输出:/*A 0 1 1 0 0 B 1 0 0 1 1 C 1 0 0 1 0 D 0 1 1 0 0 E 0 1 0 0 0*/#include#include#define MAX 10//定义邻接矩阵的结构体typedef struct{ int vexnum,arcnum原创 2016-05-04 21:30:10 · 1170 阅读 · 0 评论 -
图的邻接链表存储
//输入样例/* 5 0 AB AD AC CD BE DE *///输出/* Please Input the edge x-->y:AB AD AC CD BE DE A 1 2 3 B 0 4 C 0 3 D 0 2 4 E 1 3 */#include#includeusing namespace std;#define MAX原创 2016-05-06 16:43:04 · 628 阅读 · 0 评论 -
UVA 11997 K Smallest Sums(多路合并)
这里先把每个序列排序,然后依次和a[1]这个序列合并,那么a[1]这个序列中放的就是最终的结果了。#include#include#include#include#includeusing namespace std;#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define ss(x) scanf("%d",&x)const int m原创 2016-10-20 18:36:33 · 173 阅读 · 0 评论 -
UVA 11991 Easy Problem from Rujia Liu?(map,vector)
#include#include#include#include#include#includeusing namespace std;#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define ss(x) scanf("%d",&x)map > ha;int main(){ int n,m; while(scanf原创 2016-10-20 14:38:38 · 163 阅读 · 0 评论 -
UVA 11995 I Can Guess the Data Structure!(水题)
自己写了个超烂的代码,虽然AC了,但是太丑了,把刘如家的写了一遍。#include#include#include#include#include#includeusing namespace std;#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define ss(x) scanf("%d",&x)const int maxn=100原创 2016-10-20 14:11:17 · 155 阅读 · 0 评论 -
UVA 11988 Broken Keyboard (a.k.a. Beiju Text)(数组模拟链表)
#include#include#include#includeusing namespace std;const int maxn=100000+5;char s[maxn];int Next[maxn];int main(){ while(scanf("%s",s+1)!=EOF) { int len=(int)strlen(s+1);原创 2016-11-04 17:41:01 · 159 阅读 · 0 评论 -
图论 最短路径dijkstra算法
#include#includeusing namespace std;#define MAX 100#define INFINITY 10000typedef struct{ int arc[MAX][MAX]; int vex,edge;}AdjMatrix;void Dijkstra(AdjMatrix *G,int v0,int d[MAX]){ i原创 2016-05-12 18:23:04 · 351 阅读 · 0 评论 -
图论 最短路径Floyd算法
#include#includeusing namespace std;#define MAX 100#define INF 10000typedef int ELemtype;typedef struct{ ELemtype arc[MAX][MAX]; int vex,edge;}AdjMatrix;void intial(AdjMatrix *G){原创 2016-05-12 18:39:43 · 262 阅读 · 0 评论 -
uva 11988 Broken Keyboard (a.k.a. Beiju Text)
以前看这题看不懂,现在终于自己写出来了。。。#include#include#includeconst int MAXN=100000+10;char s[MAXN];int last,cur,next[MAXN];int main(){ while(scanf("%s",s+1)==1) { last=cur=0; next[0]原创 2016-05-23 22:00:41 · 201 阅读 · 0 评论 -
uva 442 Matrix Chain Multiplication
#include#include#include#includeusing namespace std;struct Matrix{ int a,b; Matrix(int a=0,int b=0):a(a),b(b) {};}m[26];stack s;int main(){ int n; cin>>n; for(int i=0;i<n;原创 2016-05-23 20:11:28 · 212 阅读 · 0 评论 -
邻接表的dfs遍历
//输入样例/* 5 0 AB AD AC CD BE DE *///输出/* Please Input the edge x-->y:AB AD AC CD BE DE A 1 2 3 B 0 4 C 0 3 D 0 2 4 E 1 3 *///dfs测试数据/* 8 0 Please Input the edge x-->y:AB原创 2016-05-06 18:26:47 · 916 阅读 · 0 评论 -
邻接表的bfs遍历
//输入样例/* 5 0 AB AD AC CD BE DE *///输出/* Please Input the edge x-->y:AB AD AC CD BE DE A 1 2 3 B 0 4 C 0 3 D 0 2 4 E 1 3 *///dfs测试数据/* 8 0 Please Input the edge x-->y:AB原创 2016-05-06 18:44:10 · 782 阅读 · 0 评论 -
2路插入排序
基本思想:以第一个记录为界,将整个记录序列分成两部分进行处理。将不小于第一个记录的所有记录用折半插入的方式插入左路的序列序列中,将小于第一个序列的记录插入用折半插入的方法插入到右路的序列中。final为左路最后一个元素,first为右路第一个元素。空间复杂度O(n),时间复杂度O(n^2),这是稳定的排序方法//2路插入排序#include#include#includeusing原创 2016-05-14 14:12:05 · 1043 阅读 · 0 评论 -
排序 希尔排序
增量选择的是唐纳德增量,对于增量的选择,有好多种,时间复杂度也不同主要典型的有:#include#includeusing namespace std;void ShellSort(int a[],int n){ int i; for(int D=n/2;D>0;D/=2) { for(int p=D;p<n;p++) {原创 2016-05-14 13:19:14 · 195 阅读 · 0 评论 -
链栈
链栈#includeusing namespace std;typedef int elemtype;typedef struct node{ elemtype data; node *next;}linkstack;//初始化linkstack *intial(){ linkstack *s; s=(linkstack *)malloc(size原创 2016-05-01 11:16:35 · 208 阅读 · 0 评论 -
非二叉树转二叉树的表示法
//树的存储结构,详细图解分析在 秦玉平 马靖善所编的数据结构(第三版) p137-142*/#include#define MAX 100typedef char Elemtype;//双亲表示法typedef struct{ Elemtype node; int father;}PTNode;typedef struct{ PTNode nodes[MAX原创 2016-05-01 11:15:29 · 578 阅读 · 0 评论 -
二叉搜索树
递归的实质,就是当进入下一次递归是,数学模型仍然不变,只是范围减小。以下代码主要运用尾递归。即返回指针。//测试样例:30 15 41 50 33 35 34 0//二叉搜索树#include#include#includeusing namespace std;typedef int Elemtype;typedef struct node{ Elemtype原创 2016-04-30 18:33:05 · 198 阅读 · 0 评论 -
uva 839 Not so Mobile
#include#includeusing namespace std;bool solve(int &W){ int w1,d1,w2,d2; bool b1=true,b2=true; scanf("%d%d%d%d",&w1,&d1,&w2,&d2); if(!w1) b1=solve(w1); if(!w2) b2=solve(w2);原创 2016-05-25 09:34:23 · 251 阅读 · 0 评论 -
uva 548 Tree
#include#include#includeusing namespace std;const int maxv=10000+10;int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv];int n;bool read_list(int *a){ string line; if(!getline(cin,l原创 2016-05-25 08:44:34 · 190 阅读 · 0 评论 -
图 邻接链表的拓扑排序
#include#includeusing namespace std;#define MAX 10typedef char Elemtype;typedef struct link{ int num; struct link *next;}slink;typedef struct{ struct{ Elemtype vertex;原创 2016-05-11 20:29:49 · 618 阅读 · 0 评论 -
uva 122 Trees on the level
#include#include#include#include#includeconst int MAXN=256+10;using namespace std;struct Node{ bool have_value; int v; Node *left,*right; Node():have_value(false),left(NULL),rig原创 2016-05-24 21:15:30 · 202 阅读 · 0 评论 -
uva 679 Dropping Balls
思维方式值得学习。#include#includeusing namespace std;int main(){ int t; while(scanf("%d",&t)&&t!=-1) { while(t--) { int D,I; while(scanf("%d%d",&D,&I)原创 2016-05-24 19:07:43 · 193 阅读 · 0 评论 -
uva 12657 Boxes in a Line
双向链表的两端各放一个0。#include#include#include#includeconst int MAXN=100000+10;int left[MAXN],right[MAXN];void Link(int L,int R){ right[L]=R;left[R]=L;}int main(){ int n,m,kase=0; while原创 2016-05-24 16:35:59 · 206 阅读 · 0 评论 -
链式二叉树的递归与遍历
//测试数据:EBH GAC F D #include#include#include#includeusing namespace std;typedef char Elemtype;//定义这棵树的结点typedef struct node{ Elemtype data; struct node *lchild; struct node *rc原创 2016-04-29 09:36:11 · 427 阅读 · 0 评论 -
堆栈
堆栈#includeusing namespace std;#define intial 100typedef char elemtype;typedef struct{ int top; elemtype *data; int stacksize;}stack;stack s;void intistack(stack *s) //创建一个空栈s{原创 2016-05-01 11:17:15 · 267 阅读 · 0 评论 -
二叉树的顺序存储结构
二叉树的顺序存储结构#include#includeusing namespace std;#define Virnode '0'//定义虚拟节点#define MAX_TREE_SIZE 100//定义存储空间最大量typedef char Elemtype;//定义节点类型typedef Elemtype SqBitTree[MAX_TREE_SIZE];//SqbitTree[原创 2016-05-01 11:17:58 · 553 阅读 · 0 评论 -
拓扑排序
#include#includeusing namespace std;const int maxn=100+5;int c[maxn];int n;int G[maxn][maxn];int topo[maxn];int t;int m;bool dfs(int u){ c[u]=-1; for(int v=0;v<n;v++) {原创 2016-05-26 13:52:58 · 171 阅读 · 0 评论 -
并查集
//并查集#includeint MaxSize;typedef int Elemtype;typedef struct{ Elemtype data; int parent;}SetType;//并查集查找int Find(SetType s[],Elemtype x){//MaxSize是全局变量,代表着并查集的大小 int i; for(i=0原创 2016-05-02 11:59:01 · 204 阅读 · 0 评论 -
uva 816 Abbott's Revenge
#include#include#include#include#includeusing namespace std;struct Node{ int r,c,dir; Node(int r=0,int c=0,int dir=0): r(r),c(c),dir(dir) {};};const int maxn=100;const char *dirs="NES原创 2016-05-26 09:22:05 · 235 阅读 · 0 评论