算法
文章平均质量分 61
turbobhh
这个作者很懒,什么都没留下…
展开
-
曼哈顿距离 欧几里得距离 切比雪夫距离
曼哈顿距离:在欧几里德空间的固定直角坐标系上两点所形成的线段对轴产生的投影的距离总和。例如在平面上,坐标(x1, y1)的i点与坐标(x2, y2)的j点的曼哈顿距离为:d(i,j)=|X1-X2|+|Y1-Y2|.转载 2014-05-26 16:24:09 · 3296 阅读 · 0 评论 -
pat1067 考察了逆向思维
#include#include#includeusing namespace std;int pos[100000 + 5];int N,ans=0,correct=0;set S;//存储当前位置不对的int main(){ scanf("%d",&N); for (int i = 0; i int num;原创 2014-11-27 20:55:01 · 435 阅读 · 0 评论 -
KMP算法
KMP字符串模式匹配详解来自CSDN A_B_C_ABC 网友KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法。简单匹配算法的时间复杂度为O(m*n);KMP匹配算法。可以证明它的时间复杂度为O(m+n).。一. 简单匹配算法先来看一个简单匹配算法的函数:int Index_BF ( char S [ ], char T [ ], int po转载 2015-02-04 21:04:34 · 347 阅读 · 0 评论 -
使用(x&y) + ((x^y)>>1) 求平均数
在一个面试题里见到这么一道题:下面的代码:int func (int x, int y){ return (x&y) + ((x^y)>>1);}当 x 为 729,y 为 271 时函数的返回值是多少?思路最简单也最直接的就是将 x 和 y 都先转换为二进制,然后老老实实的做按位与,按位异或等运算,最后得出结果。在分析该表达式的实现思路之前,首转载 2015-02-13 12:47:42 · 420 阅读 · 0 评论 -
最长回文子串
最长回文子串是最初我在网易笔试的时候遇见的,当时天真的把原字符串S倒转过来成为S‘,以为这样就将问题转化成为了求S和S’的最长公共子串的问题,而这个问题是典型的DP问题,我也在前面的文章中介绍了3中解决这个问题的方法。但是非常可惜,后来才知道这个算法是不完善的。那么到底为什么呢?听我慢慢道来。S=“c a b a” 那么 S' = “a b a c”, 这样的情况下 S和 S‘的最长公转载 2015-03-25 23:18:30 · 345 阅读 · 0 评论 -
pat1081 学习下strtok_r()对分隔字符串的处理(当然不用也行,scanf读取时加控制符)
#include #include #include #include #include constint MAXN=105;using namespace std;char s[MAXN*100];int N;typedef long long LL;LL numerators[MAXN],denominators[MA原创 2014-11-24 15:18:35 · 464 阅读 · 0 评论 -
哈希树
在各种介绍里的都比较抽象,其实没有那么难,这里进行一个最简单的说明。 在将一个数进行哈希的时候,我曾经写过关于哈希的这么些东西:对于数,当一个质数不够用的时候,可以加上第二个质数,用两个mod来确定该数据在库中的位置。那么这里需要简单的解释一下,对于一个质数x,它的mod有[ 0 .. x - 1 ] x种;所以对于两个质数x和y,能存储的无一重复的数据有 x *y 个,其实也转载 2015-03-13 20:58:17 · 396 阅读 · 0 评论 -
计数排序
计数排序,传说时间复杂度为0(n)的排序计数排序:今天学习了计数排序,貌似计数排序的复杂度为o(n)。很强大。他的基本思路为:1. 我们希望能线性的时间复杂度排序,如果一个一个比较,显然是不实际的,书上也在决策树模型中论证了,比较排序的情况为nlogn的复杂度。2. 既然不能一个一个比较,我们想到一个办法,就是如果我在排序的时候就知道他的位置转载 2015-03-23 17:32:44 · 286 阅读 · 0 评论 -
Bitmap
所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素。由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省。 位图是一种很特殊的数据结构,可以利用位图来排序,但是这种排序方法对输入的数据是有比较严格的要求(数据不能重复,大致知道数据的范围)。来看一个具体的例子,假设我们要对0-7内的5个元素(4,7,2,5,3)排序(这里假设这些转载 2015-04-13 23:22:07 · 391 阅读 · 0 评论 -
pat1068 01背包 排序+打印路径
#include#include#include#includeusing namespace std;int N, M;int dp[10000+5][100+5];int v[10000 + 5];int main(){ scanf("%d %d",&N,&M); for(int i = 1; i sort(v+1, v +1原创 2014-11-27 22:06:24 · 398 阅读 · 0 评论 -
uva 12563 两个最优条件的01背包,用个结构体处理
#include #include constint MAXN=60;int N,T,t[MAXN];struct Node{ int time; int num; bool operator > (constNode& other)const{ return num>other.num||(nu原创 2014-11-18 14:23:15 · 389 阅读 · 0 评论 -
uva247 传递闭包
#include #include #include #include #include #include #include using namespace std;int n,m;char name1[30],name2[30];mapstring, int> stoiMap;mapint, string> itosMap;//表示i到j是原创 2014-10-04 18:02:24 · 582 阅读 · 0 评论 -
A*入门
在看下面这篇文章之前,先介绍几个理论知识,有助于理解A*算法。 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标。这样可以省略大量无畏的搜索路径,提到了效率。在启发式搜索中,对位置的估价是十分重要的。采用了不同的估价可以有不同的效果。估价函数:从当前节点移动到目标节点的预估费用;这个估计就是启发式的。在寻路转载 2014-05-26 11:48:49 · 735 阅读 · 0 评论 -
为什么dijkstra算法处理不了带有负权值的边的图
采用dijkstra算法处理带有负权边的图时有可能出现这样一种情况:因为dijsktra算法每一次j原创 2014-05-27 10:53:10 · 9537 阅读 · 4 评论 -
uva122 数组表示二叉树,bfs
一道用数组表示二叉树的例题,不断WA,原创 2014-10-26 15:03:39 · 702 阅读 · 0 评论 -
uva 1025 dag,从当前状态开开始的最优值,
#include #include const int INF=0x7fffffff;const int maxn=440;int min(int a,int b){return aint N,T,M1,M2;int t[maxn];bool has_train[maxn][maxn][2];int main() { int _case=0;原创 2014-11-16 18:27:46 · 451 阅读 · 0 评论 -
uva10048 dijsktra(改变松弛条件)
当然这题用dijkstra#include #include #include #include #include #include #define INF 0X7FFFFFFFusing namespace std;int C,S,Q;struct Edge{ int u,v,w;};struct Node{ int w原创 2014-10-04 19:39:42 · 434 阅读 · 0 评论 -
uva548 比较经典的知道两种遍历序列构造二叉树,注意后面的dfs的回溯
#include #include #include #include using namespace std;string line;int in_order[10010],post_order[10010];struct Node{ Node* left; Node* right; int v;};No原创 2014-11-01 14:24:23 · 531 阅读 · 0 评论 -
hdu 1874 dijsktra(mlogn) +bellmanford
#include #include #include #include #include using namespace std;int N,M,S,T;struct Edge{ int u,v,w;};struct Node{ int w,u; Node(int _w,int _u):w(_w),u(_u){}原创 2014-10-03 19:03:33 · 417 阅读 · 0 评论 -
copy一个比较好的优先队列dijkstra算法模板
/*Dijkstra的算法思想:在所有没有访问过的结点中选出dis(s,x)值最小的x对从x出发的所有边(x,y),更新dis(s,y)=min(dis(s,y),dis(s,x)+dis(x,y))*/#include #include #include #include using namespace std;const int Ni = 10000转载 2015-06-01 22:15:24 · 516 阅读 · 0 评论