![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法和数据结构
千本樱-夕颜xiyan10
这个作者很懒,什么都没留下…
展开
-
组合之美
转载 2014-04-08 23:10:12 · 403 阅读 · 0 评论 -
01背包基础
source:http://blog.sina.com.cn/s/blog_6dcd26b301013810.html首先是问题描述:给定n种物品和一背包,物品i的重量是wi,其价值是pi,背包的容量是M,问如何选择装入背包中的物品总价值最大?可以这样理解:背包的背负有上限,因此在这个上限内尽可能多的装东西,并且价值越多越好。在这里我之想讨论动态规划解决这个问题的详细过程。转载 2013-10-04 21:59:39 · 502 阅读 · 0 评论 -
流水线调度问题实现(动态规划基础---------问题取自算法导论)
#include using namespace std;#define MAXN 100#define LINE 2 int factory[LINE][MAXN][LINE]; //记录流水线上每个工作站的装配时间和转换至另一条流水线的开销int cost[LINE][MAXN]; //记录开销动态规划时的最优子问题int recor原创 2013-09-19 21:52:25 · 1064 阅读 · 0 评论 -
排序算法特点速记
(1)冒泡排序冒泡排序就是把小的元素往前调或者把大的元素往后调。比较是相邻的两个元素比较,交换也发生在这两个元素之间。所以,如果两个元素相等,我想你是不会再无聊地把他们俩交换一下的;如果两个相等的元素没有相邻,那么即使通过前面的两两交换把两个相邻起来,这时候也不会交换,所以相同元素的前后顺序并没有改变,所以冒泡排序是一种稳定排序算法。(2)选择排序选择排序是给每个位置选择当转载 2013-09-18 12:55:40 · 741 阅读 · 0 评论 -
贪心算法
source:http://www.cnblogs.com/chinazhangjie/archive/2010/11/23/1885330.html顾名思义,贪心算法总是作出在当前看来最好的选择。也就是说贪心算法并不从整体最优考虑,它所作出的选择只是在某种意义上的局部最优选择。当然,希望贪心算法得到的最终结果也是整体最优的。虽然贪心算法不能对所有问题都得到整体最优解,但对许多问题它能产生整体转载 2013-09-18 10:32:39 · 743 阅读 · 0 评论 -
开放寻址法记录
#include #include using namespace std;class openAddressing{public: openAddressing():store(new int[m]), m(20), n(19){ for(int i = 0; i m; i++){ store[i] = -1; } } int insert(const int &val) {原创 2013-09-18 00:34:56 · 638 阅读 · 0 评论 -
huffman表的实现
参考了几个blog后C++实现#include #define EOF -1using namespace std;class huffman_node;class huffman;class huffman_node{public: huffman_node(const int &val, const int &power):val(val), p原创 2013-09-18 00:24:59 · 715 阅读 · 0 评论 -
排序算法总结
***********************************input.cpp*****************************************#include#include#includeusing namespace std;istream& input(istream &in, vectorint> &get){ istream_itera原创 2013-09-18 00:32:36 · 567 阅读 · 0 评论 -
code of huffman(参考)
source:http://blog.csdn.net/yinxusen/article/details/6261549这是别人写的Huffman 比我的功能全点,主要是欣赏一下别人的内存分配方式,它使用的指针动态分配 [cpp] view plaincopyprint?#include #include /* 哈夫曼编码示例程序 作者:转载 2013-09-17 00:08:57 · 546 阅读 · 0 评论 -
哈夫曼树 Huffman tree 原理
source : http://hi.baidu.com/lewutian/item/24ec90b045c16195184697761. 哈夫曼树的基本概念 哈夫曼树( Huffman )又称最优二叉树,是一类带权路径长度最短的树,有着广泛的应用。 在讨论哈夫曼树之前首先需要弄清楚关于路径和路径长度的概念。树中两个结点之间的路径由一个结点到另一结点的分支构成。两结点之间的路径长转载 2013-09-13 11:46:33 · 1288 阅读 · 0 评论 -
循环移动
将数组前K位循环左移或有移动1.逆序法#include using namespace std;const int maxn = 5;int a[maxn] = {1, 2, 3, 4, 5};void reverse(int *a, int i, int j){ int m, n; for(m = i, n = j; m < n; m++, n--){原创 2013-09-11 22:56:41 · 589 阅读 · 0 评论 -
母函数
母函数(Generating function)详解在数学中,某个序列的母函数是一种形式幂级数,其每一项的系数可以提供关于这个序列的信息。使用母函数解决问题的方法称为母函数方法。母函数可分为很多种,包括普通母函数、指数母函数、L级数、贝尔级数和狄利克雷级数。对每个序列都可以写出以上每个类型的一个母函数。构造母函数的目的一般是为了解决某个特定的问题,因此选用何种母函数视乎序列本身的特性和转载 2013-08-29 23:32:52 · 477 阅读 · 0 评论 -
Codeforces Round #196 (Div. 2)
B. Routine Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputManao has a monitor. The screen of the monitor has原创 2013-08-18 14:26:54 · 456 阅读 · 0 评论 -
圆周率宏
#define PI acos(-1.0)获得圆周率转载 2013-08-18 13:39:13 · 546 阅读 · 0 评论 -
Codeforces Round #196 (Div. 2)
A. Puzzlestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe end of the school year is near and Ms. Manana, the teac原创 2013-08-18 05:16:26 · 429 阅读 · 0 评论 -
01背包问题 (python版)
问题描述:给定N中物品和一个背包。物品i的重量是Wi,其价值位Vi ,背包的容量为C。问应该如何选择装入背包的物品,使得转入背包的物品的总价值为最大??在选择物品的时候,对每种物品i只有两种选择,即装入背包或不装入背包。不能讲物品i装入多次,也不能只装入物品的一部分。因此,该问题被称为0-1背包问题。 PS:此为不需要一定装满的背包#!/usr/bin/pythonm = int(原创 2013-10-04 23:54:45 · 1528 阅读 · 0 评论 -
LCS问题实现---------------(动态规划, 取自算法导论)
前面已经参考了几个blog和看了资料,今天自己实现下。题目以及思想:http://hzzy-010.blog.163.com/blog/static/79692381200872024242126/#include #include #include #define maxn 100using namespace std;class LCS{public:原创 2013-09-21 00:28:27 · 531 阅读 · 0 评论 -
基础迷宫问题-------------(图的BFS 题目取自算法竞赛入门)
#include #include #include #include #include using namespace std;#define maxn 100//0 set as blank, 1 set as walltypedef struct{ int i; int j;}pos;typ原创 2013-09-20 20:11:19 · 841 阅读 · 0 评论 -
Codeforces Round #241 (Div. 2) B
B. Art Uniontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA well-known art union called "Kalevich is Aliv原创 2014-04-14 19:58:59 · 511 阅读 · 0 评论 -
HASH表
第一章 基本原理1.1 概述散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。1.2 基本概念1.2.1简介如图1-1,直接寻址存在一个技术问题:如果域U很大,在一台典型计算机的可用内存容量限制原创 2014-03-16 12:22:53 · 531 阅读 · 0 评论 -
huffman的证明
source:http://mindhacks.cn/2011/07/10/the-importance-of-knowing-why-part3/以本文上篇提到的霍夫曼编码为例,第一遍看霍夫曼编码的时候是在本科,只看了算法描述,觉得挺直观的,过了两年,忘了,因为不知道为什么要把两个节点的频率加在一起看做单个节点——一件事情不知道“为什么”就会记不牢,知道了“为什么”的话便给这件事情提供了转载 2013-12-26 13:03:52 · 2482 阅读 · 0 评论 -
排列组合算法
source:http://blog.163.com/redhumor@126/blog/static/19554784201131671012554/。最近一直在考虑从m个数里面取n个数的算法。最容易理解的就是递归,但是其效率,实在不能使用。一直找寻中,今日得果算法来源与互联网组合算法 本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标转载 2013-12-23 11:32:44 · 872 阅读 · 0 评论 -
平摊分均
source:http://blog.sina.com.cn/s/blog_51cea4040100gs31.html 平时分析算法的时间复杂度时,基本上就是看大的循环语句。其实,不然,对于有些操作,通过平摊分析,可以获得对某种特定的数据结构的新认识,这种认识有助于优化设计。 1985年,Robert提出平摊分析。 在平摊分析中,在一数据结构上执行一个操作序列转载 2013-12-29 00:17:33 · 458 阅读 · 0 评论 -
C. Vasya and Robot
time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has n items lying in a line. The items are consecutively numbered b原创 2013-10-23 22:23:00 · 969 阅读 · 0 评论 -
B. Vasya and Public Transport
time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya often uses public transport. The transport in the city is of two ty原创 2013-10-23 22:18:57 · 666 阅读 · 0 评论 -
多重背包问题(悲剧解法)
多重背包:输入 n, m, m是背包容量,n是物品种类, p存储物品价值, w存放物品重量, c存放每种的数量, 同种物品各个属性索引相同方法:笨办法:将同种物品集合做不同类来处理,于是将同种类的集合也拆分为01背包问题求解#!/usr/bin/pythonm = int(raw_input("get m as the max bag size:"))n = int(r原创 2013-10-14 21:25:27 · 586 阅读 · 0 评论 -
01背包问题优化
01背包问题一维数组优化的关键在于在优化中保证状态转移方程成立,即每次计算时要保证能获取到需要的前一状态。这也是逆序的原因#!/usr/bin/python m = int(raw_input("get m as the max bag size:")) n = int(raw_input("get n as the goods tots:")) print("b原创 2013-10-12 23:26:40 · 552 阅读 · 0 评论 -
背包问题解析
source:http://blog.csdn.net/lyhvoyage/article/details/854585201 背包有n 种不同的物品,每个物品有两个属性,size 体积,value 价值,现在给一个容量为 w 的背包,问最多可带走多少价值的物品。 int f[w+1]; //f[x] 表示背包容量为x 时的最大价值转载 2013-10-13 13:36:14 · 547 阅读 · 0 评论 -
01背包问题的优化解法
先来分析01背包:01背包(ZeroOnePack):有N件物品和一个容量为V的背包。(每种物品均只有一件)第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:转载 2013-10-12 23:06:56 · 772 阅读 · 0 评论 -
完全背包问题
一:问题:有N种物品和一个容量为V的背包,每种物品都有无限件可用。第i种物品的费用是c[i],价值是w[i]。 求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大二:完全背包问题与01背包问题的唯一区别在于每个物品可以重复使用。因此不用像01背包问题使用二位数组,利用一个维来避免物品重复三:伪代码for(int i = 0 ; i原创 2013-10-08 08:21:58 · 608 阅读 · 0 评论 -
背包问题(01背包,完全背包,多重背包)
source:http://www.cnblogs.com/daoluanxiaozi/archive/2012/05/06/2486105.html2012-05-06 18:27 by 捣乱小子, 1064 阅读, 2 评论, 收藏, 编辑写在最前面的近日为以下琐事烦身: 差不多要向学院提交项目申请了,本来是想做个多模式的IM系统的,可是跟往届通过审核的项目比起转载 2013-10-08 07:19:02 · 586 阅读 · 0 评论 -
交换数据方法总结
变量a,b1,中间变量tmp = aa = bb = tmp2.与a = a^b b = a^ba =a^b3.加减a = a + bb = a - ba = a - b原创 2013-08-17 09:33:37 · 430 阅读 · 0 评论 -
最大公约数
求最大公约数的C/C++算法//更相减损法int gcd(int a,int b){ while(a!=b) { if(a>b) a-=b; else b-=a; } return a;}//辗转相除法--递归int gcd(int a,int b){ if(b==0)转载 2013-08-17 09:28:39 · 447 阅读 · 0 评论 -
uva 299
#include int cases;int len;int tot;int store[55];int main(){ int i, j; int tmp; scanf("%d", &cases); while(cases--){ scanf("%d", &len); for(i = 0; i < len; i++){ scanf("%d", &store原创 2013-08-15 22:10:48 · 364 阅读 · 0 评论 -
uva 152
#include #include #include const int maxn = 5010;struct tree{ double x; double y; double z;}trees[maxn];int count[10];int main(){ int tot, i, j; double x, y, z; tot = 0; while( 3原创 2013-08-15 22:09:52 · 492 阅读 · 0 评论 -
uva 11464
#include #include #include using namespace std;int T;int N;const int maxn = 20;const int ans = 1000000000;int A[maxn][maxn], B[maxn][maxn];int check(int i){ memset(B, 0, sizeof(B));原创 2013-08-15 22:09:04 · 421 阅读 · 0 评论 -
uva 11300
#include #include using namespace std;int n;long long int M;long long int a[1000011], b[10000011];int main(void){ while( scanf("%d", &n) == 1){ long long int tot = 0; for(int i原创 2013-08-15 22:07:49 · 385 阅读 · 0 评论 -
uva 10815
#include #include #include using namespace std;set store;int main(){ string str; while(cin >> str){ string::iterator iter= str.begin(); while(iter != str原创 2013-08-15 22:04:14 · 391 阅读 · 0 评论 -
uva 10494
#include #include #define maxn 100000 char lft[maxn];char op;long long int rht;char re[maxn];long long int mode(char *lft , long long int rht){ int i; long long int ret = 0; for(i = 0原创 2013-08-15 22:03:15 · 441 阅读 · 0 评论 -
radix trie 详解
source:http://blog.csdn.net/joker0910/article/details/8250085基数(radix)树Linux基数树(radix tree)是将指针与long整数键值相关联的机制,它存储有效率,并且可快速查询,用于指针与整数值的映射(如:IDR机制)、内存管理等。IDR(ID Radix)机制是将对象的身份鉴别号整数值ID与转载 2013-08-15 11:13:46 · 1105 阅读 · 0 评论