随笔分类 -  hash

HDU 4339 Query
摘要:算法:比赛时没有想到好的算法,暴力是O( Q * N )肯定超时。赛后,线段树,树状数组,HASH都能AC,想了下,的确用树状数组时间复杂度就可以优化到O(Q * lgN * lgN)2000msAC.。。View Code #include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<vector>#include<string>#include<math.h>#include<map>#includ 阅读全文

posted @ 2012-08-03 22:16 more think, more gains 阅读(217) 评论(0) 推荐(0)

HDU 4334 Trouble
摘要:算法:比赛时,最先想到的是O( N * N * N + N ^2 * log (N * N * N )) = O( N * N*N)的时间复杂度。N <= 200,5s中肯定不会超时。写出来提交MLE。。然后计算下内存要开800 0000数组,long long 型。M =800 0000 * 8 = 6400 0000 字节 = 61 M, 只能开4000000然后开40000数组。。时间复杂度变成O( N ^ 3 * log N )提交TLE。。泪奔,改为HASH。。O(N * N * N)静态链表处理冲突。。AC。4000多ms...时间好高,旭哈希过的1600ms..原因是我定义 阅读全文

posted @ 2012-08-02 21:51 more think, more gains 阅读(143) 评论(0) 推荐(0)

POJ 3294 Life Forms 二分 + 哈希
摘要:终于搞死这题了,先是TLE,后是MLE, 然后是WA,AC得的不容易啊。。多项式差值取模神奇的算法。。。1280msView Code #include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>#include<algorithm>#include<string>#include<vector>#include<set>using namespace std;#define Prime 113#define MOD 阅读全文

posted @ 2012-07-22 07:29 more think, more gains 阅读(156) 评论(0) 推荐(0)

poj 2002 Babelfish
摘要:算法:1.map 985ms..2.Trie树,250ms..4倍。。3.排序+二分查找 200ms左右struct dict{ char a[11]; char b[11];}dic[100001];4. HASH将字符串映射为一个HASH值,HASH函数可以从网上自己找一个用静态链表处理冲突,速度最快235msView Code /*代码自己没写了,此代码来自http://wingszero.blogbus.com/logs/60733344.html*/#include<iostream>#include<string>using namespace std;co 阅读全文

posted @ 2012-07-21 16:28 more think, more gains 阅读(153) 评论(0) 推荐(0)

poj 2022 Squares
摘要:算法:1.枚举任意两个点,根据公式计算出另外两个点坐标是否存在。2.查找算法,可以用HASH或二分查找。View Code #include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>using namespace std;struct Point{ int x,y; bool operator < ( const Point &A) const { if( x != A.x ) return x &l 阅读全文

posted @ 2012-07-21 14:35 more think, more gains 阅读(223) 评论(0) 推荐(0)

poj 1840
摘要:算法:1.分成两半部分,前面两个一组,后面三个一组。2. HASH保存前两个数和,或二分查找。View Code #include<stdio.h>#include<string.h>#include<stdlib.h>#define MAXN 12500000short int hash[26500000];int a, b, c, d, e;int main( ) { while( scanf("%d%d%d%d%d", &a, &b, &c, &d, &e) != EOF) { int cnt 阅读全文

posted @ 2012-07-21 07:31 more think, more gains 阅读(171) 评论(0) 推荐(0)

poj 3274 Gold Balanced Lineup
摘要:这题真的没有想到可以用HASH,对HASH又有了新的理解.算法:1.二分枚举,TLEView Code #include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>#include<algorithm>#include<map>#include<math.h>using namespace std;int N, k;struct node{ int bit[40];}tx[101000];int sum[40];int jugde 阅读全文

posted @ 2012-07-18 17:44 more think, more gains 阅读(154) 评论(0) 推荐(0)

poj 3349 Snowflake Snow Snowflakes
摘要:用了三种算法:第一种算法:相当暴力,用map保存所有可能得雪花,果断TLE。第二种算法:HASH, 采用拉链法处理冲突,数据结构静态链表,hash方法为雪花六个点和,相邻点之差。AC第三种算法:HASH,sum为各雪花异或再加上相邻点之差。再根据sum从小到大排序。AC第二种算法代码:View Code #include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>using namespace std;#define MAXN 1440000#define MOD 阅读全文

posted @ 2012-07-18 11:12 more think, more gains 阅读(147) 评论(0) 推荐(0)

字符串HASH
摘要:字符串哈希方法很多。当字符串很多,但是不同得却很小时候,利用字符串HASH很方便。而不用大费内存去用MAP。有时侯还可能使MLE。详细字符串HASH讲解。http://www.cnblogs.com/atlantis13579/archive/2010/02/06/1664792.htmlView Code #include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#define HASH 1000003int head[HASH];char str[1000][40 阅读全文

posted @ 2012-07-14 17:27 more think, more gains 阅读(215) 评论(0) 推荐(0)

前m大的数
摘要:#include<stdio.h>#include<stdlib.h>#include<string.h>int dp[11000],A[5100];int main( ){ int N,M,i,j,k,max; while(scanf("%d%d",&N,&M)!=EOF) { k=0;max=0; memset(dp,0,sizeof(dp)); for(i=1;i<=N;i++) scanf... 阅读全文

posted @ 2011-05-02 15:44 more think, more gains 阅读(270) 评论(0) 推荐(0)

hdu 1280用hash解决。。
摘要:首先什么是hash??“基于比较的”排序复杂度下界是O(nlogn)‏ 但对于某些情况可以更快 现有N个整数,范围在0至10000,如何排序? 建立数组int num[10001],初始化为0,num[i]表示有多少个数等于i 读入一个数a,则num[a]++ 可以达到O(n)复杂度,这个思想就是hashHash的思想n将某个对象对应到一个关键值,然后通过关键值归类,放入到一个表中(哈希表),今后可以根据关键值迅速查找 nHash可以用来判重和统计数目..根据这个思想解决hdu 1280就非常简单了。。。。。 阅读全文

posted @ 2011-04-14 17:41 more think, more gains 阅读(197) 评论(0) 推荐(0)

导航