社区讨论

听灌佬多

灌水区参与者 3已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@m1u4owxr
此快照首次捕获于
2024/10/04 10:50
去年
此快照最后确认于
2025/11/04 18:08
4 个月前
查看原帖
P3370
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 212370440130137957ll, MOD =99593441 , base = 131, base1 = 13331;
int n;
string s;

struct ha {
	int hash1, hash2;
} z[12345678];

bool cmp(ha x, ha y) {
	return x.hash1 < y.hash1;
}

int hhash(string x) {//函数名不能直接用hash,会重名;
	int cnt = 0;
	for (int i = 0; i < x.length(); i++) {
		cnt = (cnt * base1 + x[i]) % mod;
	}
	return cnt;
}

int Hash(string x) {
	int cnt = 0;
	for (int i = 0; i < x.length(); i++) {
		cnt = (cnt * base + x[i]) % MOD;
	}
	return cnt;
}

signed main(/*yzx 2024.10.4   洛谷P3370【模板】字符串哈希*/) {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> s;
		z[i].hash1 = hhash(s);
		z[i].hash2 = Hash(s);
	}
	int ans = 0;
	sort(z + 1, z + n + 1, cmp);
	for (int i = 2; i <= n; i++) {
		if (z[i].hash1 != z[i - 1].hash1 || z[i - 1].hash2 != z[i].hash2) {
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...