算法
yanghangjun
南京大学电子科学与工程系
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
邻接表
邻接表 邻接矩阵用二维数组即可存取,比较简单,但除完全图外,一般的图不是任意两个顶点都相邻接,因此邻接矩阵也有很多零元素,特别是当n 较大而边数相对完全图的边(n-1)又少得多时,邻接矩阵仍是很稀疏,这样浪费存储空间。 邻接表(Adjacency List)是图的一种顺序存储与链式存储结合的存储方法,类似于树的孩子链表表示法。由于它只考虑非零元素,因而节省了零元素所占的存储空间转载 2012-04-19 16:35:09 · 4233 阅读 · 0 评论 -
二叉树的遍历(递归实现+非递归实现)
参考代码: #include #include using namespace std; class Tree{ public: Tree():element(0),left(NULL),right(NULL){} Tree(int num):element(num){left=right=NULL;} Tree* search(int num); Tree* inser原创 2012-03-27 11:50:27 · 746 阅读 · 0 评论 -
求n个数和为m的组合数目
题目的意思是: 有n个数,给我们一个和m。要求从n个数中抽取若干个数(>=1),使得这些数的和为m。求符合条件的组合的数目。 参考代码: #include using namespace std; const int num[10] = {3,2,1,4,5,6,7,8,9,10}; const int sum = 10; const int n=10; int main()原创 2012-03-15 19:39:26 · 5022 阅读 · 2 评论 -
二叉查找树 转换成 排序的双向链表
http://www.cnblogs.com/XjChenny/archive/2011/10/01/2197783.html 自己写的代码: #include #include using namespace std; class Tree{ public: Tree():element(0),left(NULL),right(NULL){} Tree(int nu转载 2012-04-12 18:38:16 · 494 阅读 · 0 评论 -
最长递增子序列
采用动态规划。使用LIS【】保存包含当前节点的子序列的长度。 参考代码: #include using namespace std; int num[9]={ 1 ,2 ,4, 7, 4, 3, 1,8 ,1}; int find(int *array, int len,int *Pre, int *index) { int i,j; int *LIS =原创 2012-04-12 00:44:55 · 737 阅读 · 0 评论 -
在二元树中找出和为某一值的所有路径
从根到树叶, 找出所有路径中 和为给定的路径。 先序遍历,把访问到的节点放入vector中。当递归返回时从vector中删除节点。 参考代码: #include #include using namespace std; struct Tree { Tree(int num):element(num),left(NULL),right(NULL){} int el原创 2012-04-12 20:05:36 · 615 阅读 · 0 评论 -
hash表的代码
1、使用分离链接法避免collision 参考代码: #include #include #include using namespace std; class node { public: string word; int count; node * next; }; node * hashTable[700]={}; void init() { int i; for(i=0原创 2012-04-13 14:52:12 · 1024 阅读 · 0 评论 -
利用中序+后序 恢复二叉树
要恢复一个二叉树,必须要有一个中序。 利用后序得到根,再根据中序将树分成左右两份。然后递归地调用即可。 参考代码: 注意:此代码未对后序与中序的字符串有效性进行检查。 #include using namespace std; struct Node { char value; Node * left; Node * right; }; Node * reconstruct(c原创 2012-04-25 20:35:34 · 1698 阅读 · 0 评论 -
KMP字符串查找算法
查找q是否在p中。先对q进行特征统计。。。 比较挫的代码先贴上,以后有精力再改进改进。 #include using namespace std; /** 统计字串的特征 比如字串为 const char *q="abcabcd" 特征表为 int table[]={-1,0,0,0,1,2,3}; */ int * createTable(const char *q) { if(原创 2012-04-25 19:57:06 · 692 阅读 · 0 评论 -
0-1规划问题——回溯法
参考代码如下: #include const int Value[8]={11,21,31,33,43,53,55,65}; const int Weight[8]={1,11,21,23,33,43,45,55}; const int Len=8; int x[8]={0}; int cx[8]={0}; int bestP=0; int totalV=0; int total原创 2012-02-21 13:01:35 · 1449 阅读 · 0 评论 -
拓扑排序+入度检测
拓扑排序: 若有 a->b, 则要求a一定要在b之前打印出来。 思路: 找到一个入度为0的节点,打印出来,把与之相邻的节点入度-1; 重复上面工作。 参考代码: void Topsort(graph *g) { int *Indegree = new int[g->n]; int i; /*统计入度*/ for(i=原创 2012-04-19 18:51:21 · 2098 阅读 · 0 评论 -
全组合—动态规划
#include #include #include using namespace std; vector result; int hashtable[26]={0}; bool flag=false; int main() { string a; cin>>a; int len=a.length(); int i; for(i=0;i<len;i++) { int coun原创 2012-05-04 14:33:16 · 1098 阅读 · 0 评论 -
无权图最短路径
参考代码: 复杂度为O(V+E) void Unweight(struct graph *g,string v) { int *mask= new int[g->n]; int i; int n; int num=0; for(i=0;in;i++) { mask[i]=0; if(VName[i]==v) n=i; } num++; queueq; q.p原创 2012-04-19 20:28:59 · 2104 阅读 · 1 评论 -
Dijkstra最短路径算法
基本思路是:选择出发点相邻的所有节点中,权最小的一个,将它的路径设置为确定。其他节点的路径需要保存起来。然后从刚刚确认的那个节点的相邻节点,算得那些节点的路径长。然后从所有未确定的节点中选择一个路径最短的设置为确定。重复上面步骤即可。参考代码如下: void Dijkstra(graph *g,string v) { int n = g->n; int *flag = new int[n];原创 2012-04-22 18:05:16 · 905 阅读 · 0 评论 -
0-1背包问题——动态规划
如题:背包容量为17. 五种物品 W: 3,4,7,8,9 V :4,5,10,11,13 思路: 要开辟一个空间result[N][M], 其中result[i][j]表示 若只有前面i个物品,包的容量为j的话,最大的价值为多少。 递归式子为: result[i][j] = result[i-1][j] , value[i]+ result[i-1][j-weight[i] ]原创 2012-02-18 22:41:35 · 535 阅读 · 0 评论 -
只支持10以下的计算器
参考代码: #include #include using namespace std; /** return num2 opt num1 */ int cal(int num1, int num2, char opt) { switch(opt) { case '+': return num2+num1; case '-': return num2-num1; case '*':原创 2012-04-23 23:23:34 · 647 阅读 · 0 评论 -
求一堆数中三个数的和为特定值的解
我觉得比较好的方法: 对原始数据排序。将第一个数依次选举。然后在其后面求两个数的和为特定值的问题。复杂度为O(n^2) 参考代码: #include #include using namespace std; int Num[]={1,2,3,4,5,6,7,8,9,10}; int Len=10; void find1(int *num,int result,int len) { i原创 2012-05-09 19:52:50 · 2025 阅读 · 1 评论 -
活动选择问题
题目的意思是: 有一系列的活动,要求活动的时间不能有冲突。最多可以安排多少节目。 比如目前有11个活动,活动的起始时间与结束时间如下所示: 节目: 1 2 3 4 5 6 7 8 9 10 11 si: 1 3 0 5 3 5 6 8 8 2 12 f原创 2012-02-20 21:15:37 · 758 阅读 · 0 评论 -
最长公共子序列
题目:如题所示。 参考代码: #include #include const char A[]=" a1b3cd5rfg"; const char B[]=" 2ab5cd7efg"; const int lenA=10; const int lenB=10; int m[11][11]={0}; int s[11][11]={0}; void Pri(int s[11]原创 2012-02-19 15:00:44 · 508 阅读 · 0 评论 -
多项式时间算法
定义:若存在一个常数C,使得对于所有n>=0,都有|f(n)| 例如:时间复杂度为O(nlog(n))、O(n^3)的算法都是多项式时间算法,时间复杂度为O(n^log(n))、O(n!)、O(2^n)的算法是指时间算法。 一个优化问题如果已经找到了多项式时间算法,则称该问题为多项式时间可解问题,并将这类问题的集合记为P,因此多项式时间可解问题就称为P类问题。 一个问题如果没有找到多项式时转载 2012-02-27 16:33:53 · 36180 阅读 · 4 评论 -
矩阵链乘问题——求最小乘数
题目: 6个矩阵相乘,求需要乘法的最少数目。 A30X35 A35X15 A15X5 A5X10 A10X20 A20X25 (分别对应A1 A2 A3 A4 A5 A6, 后面是下标) 参考代码: #include const int P[7]= {30,35,15,5,10,20,25}; const int length=6; int m[7][7]={0};原创 2012-02-19 10:48:14 · 1113 阅读 · 0 评论 -
八皇后问题——回溯法解决
在8X8个格子里,放置8个皇后。要求这8个皇后任意两个 不在同一行,不在同一列,不在对角线上。 参考代码如下: #include #include int X[9]={0}; //X[0]是备用的。 int place(int k) { int i; for(i=1;i { if( (X[i]==X[k]) || (abs(X[i]-X[k])==abs(i-k)原创 2012-02-23 12:08:35 · 1219 阅读 · 0 评论 -
计算器(只能针对整形),支持+-*/(),不支持负数
一个计算式,比如 30 + 7 *4 - (5 + 69) /3 利用计算机计算,就需要将其化成逆波兰式。 化成逆波兰式的结果为 30 7 4 * + 5 69 + 3 / - 这样子就可以直接计算了:遇到操作符,就从栈中弹出两个数字,计算完在push到里面 过程: 30 28(遇到*) + 5 69 + 3 / - 58(遇原创 2011-12-30 23:47:30 · 722 阅读 · 0 评论 -
队列ADT实现
队列也比较的简单,先进先出。流行的做法也是用数组实现。 结构体如下: struct Queue { int capacity; int front; int rear; int size; int *num; }; 直接上参考代码了: #include #include struct Queue { int capacity; int front原创 2011-12-29 23:35:44 · 2662 阅读 · 0 评论 -
栈ADT实现
栈也比较的简单,后进先出嘛。 使用单链表实现比较简单,在表头节点后面push与pop就可以了。但是每次push或者pop均需要malloc或者free,开销可能有点大。所以流行的做法是使用数组实现栈。 栈的结构体定义: struct stack { unsigned int capacity; int topPoint; int *num; }; 支持的基本操作有:原创 2011-12-29 22:55:12 · 3382 阅读 · 0 评论 -
链表ADT详解
链表原理很简单。这里实现了单链表的ADT。主要的操作有: 节点结构提的定义: struct Node { int element; struct Node *next; }; 以及链表的常见操作 /*生成一个空表,该链表只有头节点*/ List creatList(void) /*判断一个链表是否为空,形参为头节点指针; 为空返回1,否则返回0; */ in原创 2011-12-29 14:14:18 · 5632 阅读 · 0 评论 -
对分查找
对分查找的前提是:数据已经经过排序。比较的简单,直接看代码:若找到,则返回它的小标,否则的话返回-1; 前提要求 num是由小到大排序好的。 // 关于排序的算法,后面会给出 int halfSearch(const int num[], const int N,int value) { int left =0; int right = N-1; int center;原创 2011-12-27 15:52:37 · 909 阅读 · 0 评论 -
最大子序列和问题
最大子序列和问题: 给你一堆数据,比如 -2,11,-4,13,-5,-2;叫你找出一连串的数字,要求这一连串的数字的和 是 所有一连串数字和中 的最大值。 要求给出这一连串数字的起始位置与结束位置 以及和。 算法1: 穷举法,两个变量 i 与j ,分别表示起始位置与结束位置,(所以肯定要求j>=i)。 穷举所有序列串的和;代码如下:复杂度为O(n^3) int algorith原创 2011-12-27 15:27:07 · 660 阅读 · 0 评论 -
求两个数的最大公约数(编程之美上有解答)
这里所使用的其实就是阿基里德算法,求两个数的最大公约数。 算法过程: 比如求 17与6的最大公约数 令M=17,N=6; 第一次:R=M%N=5; M=N=6; N =R=5 第二次: R=M%N = 1; M=N=5; N=R=1; 第三次: R=M%N =0, M=N=1,N=R=0。 所以结束条件为if(N==0) 返回 retur原创 2011-04-15 12:00:00 · 2889 阅读 · 0 评论 -
将中缀表达式转化成后缀表达式(逆波兰式)
<br />直接上代码:<br /> <br />/*中缀表达式 转换成 后缀表达式(逆波兰式)*/<br />#include<stdio.h><br />#include<stdlib.h><br /><br />struct Stack<br />{<br /> int Capacity;<br /> int top;<br /> char *Array;<br />};<br /><br />int get_priority(char c)<br />{<br /> if( (c=='+') ||原创 2011-04-17 22:17:00 · 1903 阅读 · 0 评论 -
桶排序
<br />/*<br />桶排序<br />*/<br />#include<stdio.h><br />#include<time.h><br />#include<stdlib.h><br />#define TOTAL 100<br />void init_num(int x[]) //产生0-99大小之间的随机数<br />{<br /> int i;<br /> srand( (unsigned)time( NULL ) );<br /> for(i=0;i<TOTAL;i++)<br />原创 2011-04-15 14:09:00 · 685 阅读 · 0 评论 -
两个一元多项式相乘,数组与链表实现
<br />/*<br />一元多项式求乘积<br />*/<br />#include<stdio.h><br />#include<time.h><br />#include<stdlib.h><br />void init_num(int x[],int y[])<br />{<br /> /*<br /> int i;<br /> srand( (unsigned)time( NULL ) );<br /> for(i=0;i<65536;i++)<br /> {<br /> srand( (un原创 2011-04-15 13:13:00 · 7076 阅读 · 2 评论
分享