[hdu 6046 hash] 矩阵Hash+鸽巢定理
来源:互联网 发布:黎东方知乎 编辑:程序博客网 时间:2024/05/18 20:10
[hdu 6046 hash] 矩阵Hash+鸽巢定理
分类:Pigeonhole Principle
Hash
Matrix Hash
1. 题目链接
[hdu 6046 hash]
2. 题意描述
给出一个随机算法,给定一个二维坐标,可以得到该点对应的值(0或者1)。可以通过这个随机算法可以确定一个
现在给你一个
数据保证小矩阵一定在大矩阵里面。
3. 解题思路
本题要求在1e6*1e6的矩形内找到一个特定的1e3*1e3的小矩形。 可以选择每隔K行或K列选出一个长宽皆为L的小矩形作为识别矩形,当识别矩形出现在输入矩形时,再进行完全匹配。根据鸽笼原理,当K+2*L<=1000时,输入矩形一定覆盖了至少一个识别矩形。这里选择L=7使得可以用一个int表示一个识别矩形。将hash结果视为随机的,则识别矩形匹配成功进入完全匹配的次数约为
(106k)2∗(103)2249 ,为极小值。
题目比较特殊,是通过随机算法得到的大矩阵,所以分布会比较均匀。
也就容易想到先识别矩形的一部分。然后一直扩大范围之类的做法。
感觉主要是很难想到用到鸽笼定理啊,看了题解之后,在纸上画一画,好像是这么个道理。我的大概步骤就是:
- 预处理:可以先对输入的
103∗103 的矩阵Hash。然后将其边长为L 的子矩形的所有Hash值,以及该子矩阵左下角点的坐标存到HashMap中。- 在大矩形中枚举
(106k)2 次小矩形。求出小矩形的Hash值。在Hash表中查询该Hash值。如果存在就枚举103∗103 的矩阵是否匹配。
这样下来,我的复杂度大概是
不过,不会怎么会怎么用
4. 实现代码
#include <bits/stdc++.h>using namespace std;typedef long long LL;typedef long double LB;typedef unsigned int uint;typedef unsigned long long ULL;typedef pair<int, int> PII;typedef pair<LL, LL> PLL;typedef pair<LB, LB> PLB;typedef vector<int> VI;const int INF = 0x3f3f3f3f;const LL INFL = 0x3f3f3f3f3f3f3f3fLL;const long double PI = acos(-1.0);const long double eps = 1e-4;template<typename T> inline void umax(T &a, T b) { a = max(a, b); }template<typename T> inline void umin(T &a, T b) { a = min(a, b); }template <typename T> inline bool scan_d (T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; //EOF while (c != '-' && (c < '0' || c > '9') ) if((c = getchar()) == EOF) return 0; sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1;}template<typename T, typename ...R> inline bool scan_d (T &ret, R& ...r) { scan_d(ret); scan_d(r...); }template<typename T> void print(T x) { static char s[33], *s1; s1 = s; if (!x) *s1++ = '0'; if (x < 0) putchar('-'), x = -x; while(x) *s1++ = (x % 10 + '0'), x /= 10; while(s1-- != s) putchar(*s1);}inline void print(char ch) { putchar(ch); }inline void print(const char s[]) { printf("%s", s); }inline void print(char s[]) { printf("%s", s); }inline void println() { putchar('\n'); }template<typename T> inline void println(T f) { print(f); println(); }template<typename T, typename ...R> void print (T f, R ...r) { print(f); putchar(' '); print (r...); }template<typename T, typename ...R> void println(T f, R ...r) { print(f); putchar(' '); print (r...); println(); }template<typename T> T randIntv(T a, T b) { return rand() % (b - a + 1) + a; } /*[a, b]*/void debug() { cout << endl; }template<typename T, typename ...R> void debug (T f, R ...r) { cout << "[" << f << "]"; debug (r...); }inline unsigned sfr(unsigned h, unsigned x) { return h >> x;}int f(LL i, LL j) { LL w = i * 1000000ll + j; int h = 0; for(int k = 0; k < 5; ++k) { h += (int) ((w >> (8 * k)) & 255); h += (h << 10); h ^= sfr(h, 6); } h += h << 3; h ^= sfr(h, 11); h += h << 15; return sfr(h, 27) & 1;}const int MAXN = 1005;const int HASH_SIZE = 3000007;const int MT = 1e6 + 1000;struct HNode { ULL hv; int nxt; int x, y;} hd[HASH_SIZE];int head[HASH_SIZE], tot;void HInit() { tot = 0; memset(head, -1, sizeof(head));}bool HQuery(ULL hv, vector<PII>& pt) { int u = hv % HASH_SIZE; bool ret = false; for(int i = head[u]; ~i; i = hd[i].nxt) { if(hd[i].hv == hv) { pt.push_back(PII(hd[i].x, hd[i].y)); ret = true; } } return ret;}void HInsert(ULL hv, int x, int y) { int u = hv % HASH_SIZE; hd[tot].hv = hv; hd[tot].x = x; hd[tot].y = y; hd[tot].nxt = head[u]; head[u] = tot ++;}const ULL SEED[2] = {131, 13331};ULL Hash[MAXN][MAXN], qz[MAXN][2];ULL getHashV(int x, int y, int nn, int mm) { assert(x - nn + 1 >= 0); assert(y - mm + 1 >= 0); return Hash[x][y] + Hash[x - nn][y - mm] * qz[mm][0] * qz[nn][1] - Hash[x - nn][y] * qz[nn][1] - Hash[x][y - mm] * qz[mm][0];}int N, M, T, L, K;char S[MAXN][MAXN];bool checkOri(int xl, int yl) { if(xl < 1 || yl < 1) return false; if(N + xl - 1 > MT || M + yl - 1 > MT) return false; for(int i = 1; i <= N; ++i) { for(int j = 1; j <= M; ++j) { if(f(i + xl - 1, j + yl - 1) != S[i][j] - '0') return false; } } return true;}ULL getOriHashV(int xl, int yl, int h) { ULL hv = 0; for(int i = 1; i <= L; ++i) { ULL temp = 0; for(int j = 1; j <= L; ++j) { temp = temp * SEED[0] + f(xl + i - 1, yl + j - 1); } hv = hv * SEED[1] + temp; } return hv;}int src[2], dst[2];bool process(int x, int y) { int nx = x + L - 1, ny = y + L - 1; if(nx > MT || ny > MT) return false; ULL oriHv = getOriHashV(x, y, L); vector<PII> pt; if(HQuery(oriHv, pt) == false) return false; int dx, dy; for(int i = 0, sz = pt.size(); i < sz; ++i) { tie(dx, dy) = pt[i]; pt[i] = PII(nx - dx + 1, ny - dy + 1); } sort(pt.begin(), pt.end()); for(int i = 0, sz = pt.size(); i < sz; ++i) { tie(dx, dy) = pt[i]; if(checkOri(dx, dy)) { dst[0] = dx, dst[1] = dy; return true; } } return false;}int main() {#ifdef ___LOCAL_WONZY___// freopen ("input.txt", "r", stdin);// freopen ("ans2.txt", "w+", stdout);#endif // ___LOCAL_WONZY___ qz[0][0] = qz[0][1] = 1; for(int i = 1; i < MAXN; ++i) { qz[i][0] = qz[i - 1][0] * SEED[0]; qz[i][1] = qz[i - 1][1] * SEED[1]; } int cas = 0; N = M = 1000; scan_d(T);// T = 100; while(T --) { for(int i = 1; i <= N; ++i) scanf("%s", S[i] + 1);// int x = randIntv(90000, 990000), y = randIntv(90000, 990000);// src[0] = x, src[1] = y;// for(int i = 1; i <= N; ++i) {// for(int j = 1; j <= M; ++j) {// S[i][j] = f(x + i - 1, y + j - 1) + '0';// }// } for(int i = 0; i <= N; ++i) Hash[i][0] = 0; for(int j = 0; j <= M; ++j) Hash[0][j] = 0; for(int i = 1; i <= N; ++i) { for(int j = 1; j <= M; ++j) { Hash[i][j] = Hash[i][j - 1] * SEED[0] + S[i][j] - '0'; } } for(int i = 1; i <= N; ++i) { for(int j = 1; j <= M; ++j) { Hash[i][j] = Hash[i - 1][j] * SEED[1] + Hash[i][j]; } } HInit(); L = 10; K = N - 2 * L; for(int i = L; i <= N; ++i) { for(int j = L; j <= M; ++j) { ULL hv = getHashV(i, j, L, L); HInsert(hv, i, j); } } bool suc = false; for(int i = 1; i <= MT; i += K) { for(int j = 1; j <= MT; j += K) { suc |= process(i, j); if(suc) break; } if(suc) break; } printf("Case #%d :%d %d\n", ++ cas, dst[0], dst[1]); }#ifdef ___LOCAL_WONZY___ cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;#endif // ___LOCAL_WONZY___ return 0;}
阅读全文
0 0
- [hdu 6046 hash] 矩阵Hash+鸽巢定理
- HDU 6046 hash 哈希
- HDU 6046 hash(鸽巢原理+unordered_map)
- HDU-6046 hash(哈希)
- bzoj1141(矩阵hash)
- 矩阵hash + KMP
- hash
- Hash
- hash
- hash
- Hash
- hash
- Hash
- HASH
- hash
- hash
- HASH
- hash
- SpringMVC接收复杂集合参数
- 工作小结
- Java中的final关键字
- 大数据学习6:HDFS的一些理解
- 数组中出现次数超过一半的数字
- [hdu 6046 hash] 矩阵Hash+鸽巢定理
- LintCode-----17.子集
- 【大数据部落】 隐马尔科夫模型(HMM)股指预测实战
- Python 编写自动化工具
- Spring集成MyBatis
- 读论文《Convolutional Neural Networks for Sentence Classification》
- Wordpress目录页怎么将完整的文章变成摘要
- 我来自外星球!!!!
- idea下字符串的长度太大