随笔分类 -  Algorithm

1

扩展哈夫曼编码
摘要:POJ1061青蛙的约会#include #include using namespace std;int gcd(long long m, long long n){ if(n == 0) return m; return gcd(n, m%n);}void Cal(lo... 阅读全文

posted @ 2015-09-04 15:58 huashunli 阅读(1796) 评论(0) 推荐(0)

非递归遍历二叉树
摘要:#include #include #include #include using namespace std;typedef struct node{ int flag; char value; struct node *lchild; struct node *rchil... 阅读全文

posted @ 2015-08-16 22:54 huashunli 阅读(148) 评论(0) 推荐(0)

洗牌算法及其证明
摘要:问题定义:给定有序序列1-n,要求将其打乱,使得每个元素在任意位置出现的概率均为1/n。程序实现:void shuffle(int *arr, int n) // n为序列中元素总数{ int idx; for(int i = 0; i < n; i++) { ... 阅读全文

posted @ 2015-08-16 16:06 huashunli 阅读(558) 评论(0) 推荐(1)

计算编辑距离
摘要:编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。例如将kitten一字转成sitting:sitten (k→s)sittin (e→i)sitting (→g)问题:找出字... 阅读全文

posted @ 2015-08-15 22:54 huashunli 阅读(289) 评论(0) 推荐(0)

八皇后问题之回溯法
摘要:#include #include #include #include #include using namespace std;#define N 20int n, sum, x[N+1]; // n:问题规模 sum:方案数目 x[]方案存储void Init(){ ... 阅读全文

posted @ 2015-08-12 19:57 huashunli 阅读(161) 评论(0) 推荐(0)

LIS/LCS/LCIS
摘要:1.最长上升子序列2.最长公共子序列对于两个序列X={x1,x2,x3...xi...},Y={y1,y2,y3...yi...}3.最长上升公共子序列以下摘自刘汝佳《最长公共上升子序列(LCIS)的O(n^2)算法》预备知识:动态规划的基本思想,LCS,LIS。问题:字符串a,字符串b,求a和b的... 阅读全文

posted @ 2015-08-12 18:20 huashunli 阅读(239) 评论(0) 推荐(0)

最小生成树
摘要:1.Prim算法:1.算法描述:1).输入:一个加权连通图,其中顶点集合为V,边集合为E;2).初始化:Vnew= {x},其中x为集合V中的任一节点(起始点),Enew= {},为空;3).重复下列操作,直到Vnew= V:a.在集合E中选取权值最小的边,其中u为集合Vnew中的元素,而v不在Vn... 阅读全文

posted @ 2015-08-11 12:50 huashunli 阅读(169) 评论(0) 推荐(0)

最短路径
摘要:1.Dijkstra算法:1.定义概览Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构... 阅读全文

posted @ 2015-08-11 12:49 huashunli 阅读(202) 评论(0) 推荐(0)

DFS/BFS
摘要:Sudoku Killer#include #include #include int zeroNum;int map[9][9];int zero[81][2]; // 存储空位坐标,dfs直接搜索这些位置void print(){ for(int i = 0; i zer... 阅读全文

posted @ 2015-08-08 16:01 huashunli 阅读(133) 评论(0) 推荐(0)

快速幂/费马小定理
摘要:1.为了理解快速幂思想,先看一个例子:求5 ^ 19 = ? 因为19(10) = 10011(2),所以有5 ^ 19 = 5 ^ (1+2+16) = (5^1) * (5^2) * (5^16); 将二进制分解思想实现,代码如下:#include int QuickPow(int p, int... 阅读全文

posted @ 2015-07-21 09:38 huashunli 阅读(442) 评论(0) 推荐(0)

快速排序/归并排序
摘要:#include #include #include #include int *array;int cnt, head, tail;void random(int cnt, int head, int tail){ srand((unsigned)time(NULL)); for(in... 阅读全文

posted @ 2015-07-19 22:45 huashunli 阅读(152) 评论(0) 推荐(0)

卡塔兰数
摘要:出栈次序问题:一个栈的进栈序列为1,2,3……n,有多少种不同的出栈序列?#include #include #include using namespace std;#define N 100int len, num[2], arr[N]; // num[2]分别记录剩余的入栈出栈操作次数st... 阅读全文

posted @ 2015-07-16 12:06 huashunli 阅读(206) 评论(0) 推荐(0)

三分法
摘要:1.三分法模板double Calc(Type a){ /* 根据题目的意思计算 */}void Solve(void){ double Left, Right; double mid, midmid; double mid_value, midmid_value; L... 阅读全文

posted @ 2015-07-16 12:00 huashunli 阅读(298) 评论(0) 推荐(0)

哈弗曼编码
摘要:#include #include #include using namespace std;#define MAXNUM 10#define MAXBIT 10#define MINWEIGHT 1000000typedef struct{ char value; ... 阅读全文

posted @ 2015-07-16 10:57 huashunli 阅读(162) 评论(0) 推荐(0)

树状数组
摘要:适用条件:给定一个规模较大的数组,其中元素变更频繁,随时对数组中所有数的求和。图中底层为给定数组a,上层为辅助求解的树状数组c,二者之间的关系为 :当c的下标i为奇数时,c[i] = a[i];当c的下标i为偶数时,取决于i的因子中最大的一个“2的整数次幂”是多少,如6的因子中为2,则c[6] = ... 阅读全文

posted @ 2015-07-13 17:30 huashunli 阅读(117) 评论(0) 推荐(0)

字符串匹配
摘要:KMP#include #include #define N 102int len1, len2;char des[N], pat[N];int next[N], ans;void Init(){ scanf("%d%d", &len1, &len2); getchar(); fo... 阅读全文

posted @ 2015-07-13 14:39 huashunli 阅读(135) 评论(0) 推荐(0)

二分图匹配
摘要:1.相关概念理解:二分图:是这样一个图,它的顶点可以分为X和Y两个集合,所有边关联的两个顶点恰好分属于X和Y;二分图匹配:给定二分图G,在G的一个子图M中,M的边集中任意两条边都不依附于同一个顶点,则称M是一个匹配;最大匹配:包含边数最多的匹配称为最大匹配;完美匹配:若所有点都在匹配边上,则称这个最... 阅读全文

posted @ 2015-07-13 10:01 huashunli 阅读(213) 评论(0) 推荐(0)

并查集
摘要:用并查集查找根节点,包括三种方法:1.朴素查找法:int find(int x){ int r = x; while(father[r] != r) r = father[r]; return r;}2.路径压缩(递归):int find(int x){ if(... 阅读全文

posted @ 2015-07-12 22:12 huashunli 阅读(168) 评论(0) 推荐(0)

素数筛选
摘要:素数筛选目的是筛选出某一区间[m, n)内的所有素数,常见方法包括如下几种:1.朴素的筛选法:先写出判断函数isPrime(),再对区间内的数依次调用isPrime()进行判断,算法核心是以2~根号n作为除数。#include bool isPrime(int n){ /* C+... 阅读全文

posted @ 2015-07-12 15:01 huashunli 阅读(230) 评论(0) 推荐(0)

背包问题
摘要:------------------- 0-1背包-------------------问题描述:N件物品和容量为V的背包;每种物品均只有一件,且第i件物品重量为weight[i],价值为value[i]。求将哪些物品放入背包可使物品重量总和不超过背包容量,且价值总和达到最大?解题思路:首先定义问题... 阅读全文

posted @ 2015-07-06 16:11 huashunli 阅读(163) 评论(0) 推荐(0)

1

导航