刚刚做了poj3349和poj3274, 都是很水的题, 明天还要再做一些hash的题, 因为我这方面总是有问题。 对了我最大的问题还是搜索!!!巨简单的搜索题我就是写不对, 简直是不能要了!!!明天做poj2531,poj1416,poj2676,1129这几道搜索题吧, 必须多练习这些基础的东西, 其实真正比赛的时候那些高深的算法基本上是没有什么用的, 学那些也只是为了提高思维的深度和广度而已。
言归正传, 基础的hash还是非常的简单的,主要就是找一个适合mod就行了, 还有就是数组最好是动态建立的, 就要用vector了, 我以前没有用过这个的。主要内容就是:
头文件:#include <vector>
申请:vector<int>hash[mod];
循环时的变量: for(vector<int>::size_type j = 0; j < hash[i].size(); j ++)
加入: hash[i].push_back(j);
cplusplus中的
Modifiers:
-
assign
- Assign vector content (public member function )
-
push_back
- Add element at the end (public member function )
-
pop_back
- Delete last element (public member function )
-
insert
- Insert elements (public member function )
-
erase
- Erase elements (public member function )
-
swap
- Swap content (public member function )
-
clear
- Clear content (public member function )
-
emplace
- Construct and insert element (public member function )
-
emplace_back
- Construct and insert element at the end (public member function )
然后贴下这两个代码
poj3349
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#define MAXX 10000005
#define MAXN 100005
#define mod 900001
using namespace std;
int n, snow[MAXX][6];
vector<int>hash[mod + 5];
bool Same(int a, int b){
for(int i = 0; i < 6; i ++){
if((snow[a][1] == snow[b][(i + 1)%6] && snow[a][2] == snow[b][(i + 2)%6] && snow[a][3] == snow[b][(i + 3)%6] && snow[a][4] == snow[b][(i + 4)%6] && snow[a][5] == snow[b][(i + 5)%6] && snow[a][0] == snow[b][(i + 6)%6]) ||
(snow[a][1] == snow[b][(i + 1) % 6] && snow[a][2] == snow[b][i] && snow[a][3] == snow[b][(i + 5) % 6] && snow[a][4] == snow[b][(i + 4) % 6] && snow[a][5] == snow[b][(i + 3)% 6] && snow[a][0] == snow[b][(i + 2) % 6])
) return 1;
} return 0;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++){
int ll = 0;
for(int j = 0; j < 6; j ++){
scanf("%d", &snow[i][j]);
ll += snow[i][j];
ll %= mod;
}
for(vector<int>::size_type j = 0; j < hash[ll].size(); j ++){
// printf("%d %d\n", hash[ll][j], i);
if(Same(hash[ll][j], i)){
puts("Twin snowflakes found."); return 0;
}
} hash[ll].push_back(i);
}
puts("No two snowflakes are alike.");
//system("pause");
return 0;
}
poj3274
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#define mod 99911
using namespace std;
int n, k, f[100005][32], sum[100005][32], c[100005][32], maxx;
vector<int>hash[mod + 5];
bool cmp(int a, int b){
for(int j = 1; j <= k; j ++)
if(c[a][j] != c[b][j])return 0;
return 1;
}
void hashh(int ci){
int key = 0;
for(int i = 2; i <= k; i ++)
key += c[ci][i];
key = abs(key) % mod;
for(vector<int>::size_type j = 0; j < hash[key].size(); j ++){
if(cmp(ci, hash[key][j])){
int dis = ci - hash[key][j];
maxx = max(maxx, dis);
return ;
}
}
hash[key].push_back(ci);
}
int main()
{
scanf("%d%d", &n, &k);
int a;
for(int i = 1; i <= n; i ++){
scanf("%d", &a);
for(int j = 1; j <= k; j ++){
f[i][j] = a % 2; a /= 2;
sum[i][j] = sum[i - 1][j] + f[i][j];
if(j == 1)continue;
c[i][j] = sum[i][j] - sum[i][1];
}
}
for(int i = 0; i <= n; i ++)hashh(i);
printf("%d\n", maxx);
//system("pause");
return 0;
}