
算法导论
文章平均质量分 70
shakingWaves
这个作者很懒,什么都没留下…
展开
-
rabin-karp二维字符数组匹配
#include #include #include using namespace std;void two_d_rabin_karp_matcher(char **T,int n1,int n2,char **P,int m1,int m2,int d,int q)//二维模式匹配{ int *pattern=new int[m2]; int *text=new int[n2]原创 2014-06-20 16:36:49 · 2123 阅读 · 0 评论 -
最长回文子序列
#include #include using namespace std;void longest_palindrome(char *s,int **p,int length){ for(int i=0;i<length-1;++i){//初始化数组相关信息 p[i][i]=1; int j=i+1; if(s[i]==s[j]){ p[i][j]=2; }el原创 2014-03-15 14:54:35 · 917 阅读 · 0 评论 -
最优二叉搜索树
#include #include #include#include #include #include using namespace std;using namespace boost::timer;int compare(const void *a,const void *b)//qsort比较函数{ if ( *(double*)a < *(double*)b ) r原创 2014-03-14 16:45:56 · 873 阅读 · 0 评论 -
矩阵链乘法
#include #include #include using namespace std;using namespace boost::timer;void matrix_multiply(int **a,int **b,int **c,int m,int n,int q)//矩阵乘法{//m,n表示矩阵a的行数和列数,n,q表示矩阵b的行数和列数,c的行数和列数应该为m,q原创 2014-03-07 10:59:53 · 722 阅读 · 0 评论 -
最长单调递增子序列
#include #include #include#include using namespace std;using namespace boost::timer;void lis(int *a,int *s,int *b,int n)//Longest Increasing Subsequence,最长单调递增子序列{ for(int l=1;l<n;++l){//最长子序原创 2014-03-11 14:47:58 · 738 阅读 · 0 评论 -
最长公共子序列
#include #include #include using namespace std;using namespace boost::timer;void lcs_length(char *a,int m,char *b,int n,int **c)//最长公共子序列{//自底向上法 for(int i=1;i<=m;++i){ for(int j=1;j<=n;++j)原创 2014-03-09 21:01:56 · 774 阅读 · 0 评论 -
钢条切割-动态规划
#include #include #include using namespace std;using namespace boost::timer;int cut_rod(int *p,int n,int *result){ if(n==0) return 0; int q =numeric_limits::min();//q初始为最小值 for(int i=0;i<n;原创 2014-03-04 15:11:57 · 833 阅读 · 0 评论 -
两个最接近的数之间的差值-红黑树-快速排序
#include#include #include using namespace std;typedef struct rb_node{//带父节点的红黑树 int value; rb_node *left,*right,*parent; bool rb;//rb=0表示结点,rb=1表示红结点 int min,max,min_gap;//计算红黑树中两个最接近的数的差值}r原创 2014-03-02 20:59:34 · 1477 阅读 · 0 评论 -
Josephus排列-红黑树实现-链表实现
#include #includeusing namespace std;typedef struct list{ int value; list *next;}list, *pList;pList build_cirle_list(int n)//创建单向循环链表{ pList head=new list; pList pl,pp=head; for(int i=0;i<原创 2014-02-28 19:31:46 · 975 阅读 · 0 评论 -
二叉搜索树的插入和删除
#include#include using namespace std;typedef struct node{//带父节点的二叉树 int value; node *left,*right,*parent;}node,*pNode;void binary_tree_insert(pNode &pn,pNode pz)//二叉树插入操作{ pNode y=NULL; p原创 2014-02-17 15:07:53 · 941 阅读 · 0 评论 -
二叉查找树前驱、后继,非递归不使用栈不使用访问标记的中序遍历,最大最小节点
#include#include using namespace std;typedef struct node{//带父节点的二叉树 int value; node *left,*right,*parent;}node,*pNode;void build_binary_search_tree(pNode &pn,int n)//建立n个具有父节点的二叉搜索树{ int va原创 2014-02-16 16:22:36 · 994 阅读 · 0 评论 -
红黑树-逆序对
#include#include #include using namespace std;typedef struct rb_node{//带父节点的二叉树 int value; rb_node *left,*right,*parent; bool rb;//rb=0表示结点,rb=1表示红结点 int size;//存储左右子树大小和该结点之和}rb_node,*pRBNo原创 2014-02-25 22:02:11 · 769 阅读 · 0 评论 -
红黑树的插入删除-红黑树动态顺序统计
#include#include #include using namespace std;typedef struct rb_node{//带父节点的二叉树 int value; rb_node *left,*right,*parent; bool rb;//rb=0表示结点,rb=1表示红结点 int size;//存储左右子树大小和该结点之和}rb_node,*pRBNo原创 2014-02-25 20:53:32 · 974 阅读 · 0 评论 -
AVL数的插入
#include#include using namespace std;typedef struct avl_node{//AVL树 int value; int height; avl_node *left,*right,*parent;}avl_node,*pAvl_node;void left_rotation(pAvl_node &pn,pAvl_node px)原创 2014-02-23 15:53:10 · 926 阅读 · 0 评论 -
队列和堆栈实现二叉树的遍历
#include#include #include using namespace std;typedef struct node{//二叉树节点 int value; node *left; node *right;}node,*pnode;typedef struct list{//队列的基本结构 pnode pn; list *next;}list,*plist;原创 2014-01-29 10:23:20 · 1248 阅读 · 0 评论 -
算法导论9-2-c-带权中位数
#include #include #include #include using namespace boost::timer;using namespace std;int partition(double *a,int low,int high){ double key=a[high]; int p=low-1; double tmp; for(int i=low;i原创 2014-01-18 21:02:29 · 1790 阅读 · 1 评论 -
整齐打印
#include #include #include #include #include #include #include using namespace std;const static int M = 80;void print_neatly(vector&vec,int **extras,int **lc,int *c,int *p,int n)//整齐打印{ fo原创 2014-03-18 09:06:26 · 991 阅读 · 0 评论 -
双调欧几里得旅行商问题
#include #include #include using namespace std;typedef struct point{ int x,y;}point,*ppoint;void quick_sort_partition(ppoint *pp,int low,int high)//快速排序{ while (low<high){ ppoint pKey=pp[原创 2014-03-17 13:29:45 · 1060 阅读 · 0 评论 -
算法导论32.1-4-带间隔符的pattern匹配字符串
#include #include #include #include using namespace std;//算法导论第三版32.1-4void string_matcher(string &strSrc,vectorvecStr,int index,int pos,vector &vec_result)//查找所有出现的匹配模式{ int depth=vecStr.size原创 2014-06-17 16:27:38 · 1756 阅读 · 0 评论 -
邻接矩阵计算节点对最短路径
#include #include using namespace std;void extend_shortest_paths(int **matrix,int **W,int **result,int n)//计算最短路径{ for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ int min=numeric_limits::max();原创 2014-05-29 12:03:47 · 3761 阅读 · 0 评论 -
有限自动机字符串匹配_KMP算法计算状态转换表
#include #include #include using namespace std;int * compute_prefix_function(string str)//计算字符串的前缀函数{ int m=str.length(); int q=-1; int *prefix_table=new int[m]; prefix_table[0]=q; for(int原创 2014-06-23 21:20:17 · 1705 阅读 · 0 评论 -
最小平均完成时间调度问题_最小化完成时间
#include #include #include using namespace std;typedef struct list{ int id; int left_time; list *next;}list,*plist;typedef struct queue{ list *front; list *tail;}queue,*pqueue;void ini原创 2014-04-12 16:34:01 · 6254 阅读 · 0 评论 -
斐波那契堆抽取最小值_关键字减值_删除
#include #include #include #include using namespace std;typedef struct fib_heap_node{//斐波那契堆结点 int key;//结点值 int degree;//结点度 fib_heap_node *parent;//结点的父结点 fib_heap_node *left;//结点的左兄弟,循环双链表原创 2014-04-27 10:21:42 · 2321 阅读 · 0 评论 -
哈夫曼编码
#include #include #include using namespace std;typedef struct min_heap{//最小堆 int value; min_heap *left,*right;//左右结点}min_heap,*pmin_heap;typedef struct list{//双向链表实现栈 int value; list *next原创 2014-04-10 09:38:58 · 1245 阅读 · 0 评论 -
图的拓扑排序—递归—迭代
#include #include #include using namespace std;typedef struct Link_graphic{//邻接表 int value; Link_graphic *next;}Link_graphic,*pLink_graphic;typedef struct Queue{//队列 Link_graphic *front,*ta原创 2014-03-27 10:39:06 · 1185 阅读 · 0 评论 -
分数背包问题
#include#include #include#include using namespace std;int partition(double *ration,int *v,int *w,int low,int high)//快速排序子程序{ double pivot=ration[high]; int k=low-1; double tmp; int tmp2; f原创 2014-04-08 13:22:09 · 1207 阅读 · 0 评论 -
活动选择问题变形_值之和最大的兼容活动子集
#include#include #includeusing namespace std;void dynamic_max_value_activity_selector(int *s,int *f,int *val,int **c,int **act,int n)//动态规划-自底向上{ for(int i=0;i<n;++i){ c[i][i]=0;//如果只有一个元素,则两原创 2014-04-06 13:40:24 · 1948 阅读 · 0 评论 -
按照主关键字和次关键字排序
#include#includeusing namespace std;void quick_sort(int *s,int *f,int low,int high)//快速排序,安装活动的开始时间升序排序{ while (low<high){ int pivot=s[high]; int k=low-1; int tmp; for(int i=low;i<high;+原创 2014-04-05 14:11:43 · 4262 阅读 · 0 评论 -
最少教室问题与区间图着色问题类似
#include#include #includeusing namespace std;typedef struct list{ int tag;//教室编号 int time;//活动的结束时间 list *next;//下一个活动}list ,*plist;void minimum_lecture_hall(plist &busing,plist &free,int *原创 2014-04-05 14:00:10 · 1088 阅读 · 0 评论 -
B树的插入和删除
#include #include using namespace std;#define BTree_T 2#define BTree_N ((2*BTree_T)-1)//BTree_T 表示B树的度,BTree_N表示B树的最大关键字个数typedef struct BTree{ int keyNum;//保存关键字的个数 int key[BTree_N];//存储关键字原创 2014-04-19 21:00:24 · 1836 阅读 · 0 评论 -
活动选择问题
#include#include using namespace std;void recursive_activity_selector(int *s,int *f,int l,int h)//递归贪心算法{ int m=l+1; while (m<=h && s[m]<f[l]){ ++m; } if(m<h){ cout<<m<<"\t"; recursive_原创 2014-04-04 10:33:06 · 752 阅读 · 0 评论 -
算法导论_不相交集合
#include #include using namespace std;//不相交集合森林typedef struct disjoint_set_forest_node{ int value;//结点值 disjoint_set_forest_node *parent;//父亲结点 int rank;//结点的秩}disjoint_set_forest_node,*pDisjo原创 2014-05-03 11:23:05 · 1329 阅读 · 0 评论 -
强连通分量
#include #include #include using namespace std;typedef struct Link_graphic{//邻接表 int value; Link_graphic *next;}Link_graphic,*pLink_graphic;typedef struct Queue{//队列 Link_graphic *front,*ta原创 2014-03-31 11:01:00 · 717 阅读 · 0 评论 -
算法导论10.2-8-用一个整数地址替代前后指针实现双向链表
#include#include #include using namespace std;typedef struct dlist{//双向链表 int value;//存储值 unsigned int np;//存储地址值}dlist, *pdlist;void insert(pdlist &d,pdlist cur)//双链表插入元素{ d->np^=(unsigne原创 2014-01-26 14:32:00 · 1108 阅读 · 0 评论 -
双向链表插入删除
#include#include #include using namespace std;typedef struct dlist{//双向链表 int value; dlist *next; dlist *prev;}dlist, *pdlist;void insert(pdlist &d,pdlist cur)//双链表插入元素{ if(!d){ d=cur;原创 2014-01-24 19:32:05 · 731 阅读 · 0 评论 -
斐波那契数列
递归法,迭代法,近似值法#include #include using namespace boost::timer;using namespace std;double fib( double n){ if(n<=1) return n; else return fib(n-1) + fib(n-2);}double iteratorfib(double n){原创 2013-12-20 21:56:49 · 856 阅读 · 0 评论 -
算法导论-6.5-9 使用最小堆完成k路归并算法
//基于最小堆的K路归并算法#include #include #include using namespace std;typedef struct node{ int value; node *next;}node,*pNode;void insert_node(node *head, node *nenode)//直接插入法,插入新节点{ node *cur=hea原创 2014-01-05 15:05:27 · 2286 阅读 · 1 评论 -
两种快速排序
#include #include #include #include using namespace boost::timer;using namespace std;void same_elem_quick_sort(int *a,int low,int high)//考虑相同元素的情况{ if(low<high){ int pivot=a[low]; int i=原创 2014-01-05 10:14:55 · 612 阅读 · 0 评论 -
杨氏矩阵
#include#include #include using namespace std;void young_tableau(int **a,int i,int j,int m,int n)//维护杨氏矩阵{ int min_row=i,min_col=j; if(i+1a[i+1][j])){//比较当前元素与下边元素值的大小 min_row=i+1; } if(i<原创 2014-01-05 10:16:03 · 853 阅读 · 0 评论 -
矩阵乘法
《算法导论》第三版 P43-44#include #include #include using namespace boost::timer;using namespace std;int addrARA[4]={0,0,1,1},addrACA[4]={0,0,0,0}, addrBRA[4]={0,0,0,0},addrBCA[4]={0,1,0,1};int addrA原创 2013-12-20 14:21:01 · 644 阅读 · 0 评论