社区讨论
神秘哈希求问
P4503[CTSC2014] 企鹅 QQ参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mk6v967f
- 此快照首次捕获于
- 2026/01/09 20:42 上个月
- 此快照最后确认于
- 2026/01/09 21:18 上个月
WA 代码
CPP#include <bits/stdc++.h>
using namespace std;
int n,m,s;
char ch[30005][205];
long long pow1[205],pow2[205],hash1[30005][205],hash2[30005][205],ans,now;
#define mod1 1000000007
#define mod2 1000000009
#define ha1 13131
#define ha2 13331
#define get1(i,l,r) ((hash1[i][r] + mod1 - hash1[i][l-1] * pow1[r-l+1] % mod1) % mod1)
#define get2(i,l,r) ((hash2[i][r] + mod2 - hash2[i][l-1] * pow2[r-l+1] % mod2) % mod2)
#define endl "\n"
struct ppp{
long long d1,d2,d3,d4;
}b[30005];
bool cmp(ppp xxx,ppp yyy){
if(xxx.d1 < yyy.d1) return 1;
if(xxx.d1 > yyy.d1) return 0;
if(xxx.d2 < yyy.d2) return 1;
if(xxx.d2 > yyy.d2) return 0;
if(xxx.d3 < yyy.d3) return 1;
if(xxx.d3 > yyy.d3) return 0;
if(xxx.d4 < yyy.d4) return 1;
if(xxx.d4 > yyy.d4) return 0;
return 0;
}
#define equal(x,y) (b[x].d1 == b[y].d1 && b[x].d2 == b[y].d2 && b[x].d3 == b[y].d3 && b[x].d4 == b[y].d4)
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m >> s;
pow1[0] = pow2[0] = 1;
for(int i = 1;i <= 200;++i) pow1[i] = (pow1[i-1] * ha1) % mod1,pow2[i] = (pow2[i-1] * ha2) % mod2;
for(int i = 1;i <= n;++i){
for(int j = 2;j <= m+1;++j) cin >> ch[i][j];
for(int j = 1;j <= m+2;++j) hash1[i][j] = (hash1[i][j-1] * ha1 + ch[i][j]) % mod1;
for(int j = 1;j <= m+2;++j) hash2[i][j] = (hash2[i][j-1] * ha2 + ch[i][j]) % mod2;
}
for(int i = 2;i <= m+1;++i){
for(int j = 1;j <= n;++j){
long long x1,x2,x3,x4;
x1 = get1(j,1,i-1),x2 = get1(j,i+1,m+2),x3 = get2(j,1,i-1),x4 = get2(j,i+1,m+2);
b[j] = {x1,x2,x3,x4};
//cout << j << " " << i << " " << x1 << " " << x2 << " " << x3 << " " << x4 << endl;
}
sort(b+1,b+n+1,cmp),now = 1;
for(int j = 2;j <= n;++j){
if(equal(j-1,j)) ans += now,++now;
else now = 1;
}
}
cout << ans;
return 0;
}
AC 代码
CPP#include <bits/stdc++.h>
using namespace std;
int n,m,s;
char ch[30005][205];
long long pow1[205],pow2[205],hash1[30005][205],hash2[30005][205],ans,now;
#define mod1 1000000007
#define mod2 1000000009
#define ha1 13131
#define ha2 13331
#define get1(i,l,r) ((hash1[i][r] + mod1 - hash1[i][l-1] * pow1[r-l-1] % mod1) % mod1)
#define get2(i,l,r) ((hash2[i][r] + mod2 - hash2[i][l-1] * pow2[r-l-1] % mod2) % mod2)
#define endl "\n"
struct ppp{
long long d1,d2,d3,d4;
}b[30005];
bool cmp(ppp xxx,ppp yyy){
if(xxx.d1 < yyy.d1) return 1;
if(xxx.d1 > yyy.d1) return 0;
if(xxx.d2 < yyy.d2) return 1;
if(xxx.d2 > yyy.d2) return 0;
if(xxx.d3 < yyy.d3) return 1;
if(xxx.d3 > yyy.d3) return 0;
if(xxx.d4 < yyy.d4) return 1;
if(xxx.d4 > yyy.d4) return 0;
return 0;
}
#define equal(x,y) (b[x].d1 == b[y].d1 && b[x].d2 == b[y].d2 && b[x].d3 == b[y].d3 && b[x].d4 == b[y].d4)
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m >> s;
pow1[0] = pow2[0] = 1;
for(int i = 1;i <= 200;++i) pow1[i] = (pow1[i-1] * ha1) % mod1,pow2[i] = (pow2[i-1] * ha2) % mod2;
for(int i = 1;i <= n;++i){
for(int j = 2;j <= m+1;++j) cin >> ch[i][j];
for(int j = 1;j <= m+2;++j) hash1[i][j] = (hash1[i][j-1] * ha1 + ch[i][j]) % mod1;
for(int j = 1;j <= m+2;++j) hash2[i][j] = (hash2[i][j-1] * ha2 + ch[i][j]) % mod2;
}
for(int i = 2;i <= m+1;++i){
for(int j = 1;j <= n;++j){
long long x1,x2,x3,x4;
x1 = get1(j,1,i-1),x2 = get1(j,i+1,m+2),x3 = get2(j,1,i-1),x4 = get2(j,i+1,m+2);
b[j] = {x1,x2,x3,x4};
//cout << j << " " << i << " " << x1 << " " << x2 << " " << x3 << " " << x4 << endl;
}
sort(b+1,b+n+1,cmp),now = 1;
for(int j = 2;j <= n;++j){
if(equal(j-1,j)) ans += now,++now;
else now = 1;
}
}
cout << ans;
return 0;
}
这两段代码唯一的区别就是
按照我对 Hash 的理解不应该就是
get 获取哈希值的时候一个写的 pow[r-l+1],一个写的 pow[r-l-1],但为什么第一个错了第二个对了?按照我对 Hash 的理解不应该就是
pow[r-l+1] 吗?回复
共 0 条回复,欢迎继续交流。
正在加载回复...