poj 3274

hash的算法倒是没神马收获,倒是感觉这两天看clean code很管用,调试代码的时间大幅度下降了。

错啊,要是hash函数写的好的话,会快很多的。

附更快版本的hash函数,简单的修改即可。

1 int hashval(int pos) {
2     int res = 0;
3     for (int i = 1; i < k; i++) {
4         res = (res + dp[pos][i] + maxarray - dp[pos][0]) % maxarray;
5     }
6     return res;
7 }

 

  1 #include <iostream>
  2 #include <string>
  3 #include <vector>
  4 #include <cstdlib>
  5 #include <cmath>
  6 #include <map>
  7 #include <algorithm>
  8 #include <list>
  9 #include <ctime>
 10 #include <set>
 11 #include <string.h>
 12 #include <queue>
 13 #include <cstdio>
 14 using namespace std;
 15 #define CLR(arr, what) memset(arr, what, sizeof(arr))
 16 int n, k;
 17 const int maxarray = 100001;
 18 int dp[maxarray][32];
 19 vector<int> dic[maxarray];
 20 void save(int pos, int num) {
 21     if (0 == pos) {
 22         for (int a = 0; a < k; a++) {
 23             if (num & (1 << a)) {
 24                 dp[pos][a] = 1;
 25             }
 26         }
 27     } else {
 28         for (int a = 0; a < k; a++) {
 29             if (num & (1 << a)) {
 30                 dp[pos][a] = dp[pos - 1][a] + 1;
 31             } else {
 32                 dp[pos][a] = dp[pos - 1][a];
 33             }
 34         }
 35 
 36     }
 37 }
 38 int hashval(int pos) {
 39     int res = 0;
 40     for (int i = 1; i < k; i++) {
 41         res = (res + dp[pos][i] + maxarray - dp[pos][i - 1]) % maxarray;
 42     }
 43     return res;
 44 }
 45 bool check(int pos1, int pos2, int res) {
 46     bool judge = true;
 47     if (pos2 - pos1 <= res) {
 48         return false;
 49     }
 50     int diff = dp[pos1][0] - dp[pos2][0];
 51     for (int i = 1; i < k; i++) {
 52         judge = judge && ((dp[pos1][i] - dp[pos2][i]) == diff);
 53         if (!judge) {
 54             return false;
 55         }
 56     }
 57     return true;
 58 }
 59 int maxedge(vector<int> var, int res) {
 60     int sz = var.size();
 61     bool judge;
 62     for (int i = 0; i < sz; i++) {
 63         for (int j = i + 1; j < sz; j++) {
 64             judge = check(var[i], var[j], res);
 65             if (judge) {
 66                 res = max(res, var[j] - var[i]);
 67             }
 68         }
 69     }
 70     return res;
 71 }
 72 int maxedgestart(int res) {
 73     bool judge;
 74     for (int i = res; i < n; i++) {
 75         judge = true;
 76         for (int j = 1; j < k; j++) {
 77             if (dp[i][j] != dp[i][j - 1]) {
 78                 judge = false;
 79                 break;
 80             }
 81         }
 82         if (judge) {
 83             res = i + 1;
 84         }
 85     }
 86     return res;
 87 }
 88 int main() {
 89     int tmp;
 90     cin >> n >> k;
 91     for (int i = 0; i < n; i++) {
 92         scanf("%d", &tmp);
 93         save(i, tmp);
 94     }
 95     int curhash;
 96     for (int i = 0; i < n; i++) {
 97         curhash = hashval(i);
 98         dic[curhash].push_back(i);
 99     }
100     int res = 0;
101     res= max(res, maxedgestart(res));
102     for (int i = 0; i < maxarray; i++) {
103         res = max(res, maxedge(dic[i], res));
104     }
105     cout << res << endl;
106     return 0;
107 }

from kakamilan

 

posted on 2013-05-18 12:31  kakamilan  阅读(154)  评论(0编辑  收藏  举报

导航