【BZOJ3555】[Ctsc2014]企鹅QQ【Hash】

【题目链接】

姿势get√,判断一堆字符串是否相同,可以先hash,再按hash排序判断...这样复杂度从O(n^2L)降为O(L+nlogn+n)

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

typedef unsigned long long ULL;

const int maxn = 30005, maxl = 205;

int n, m, s;
ULL hash[maxn][maxl], hsah[maxn][maxl], tmp[maxn];
char str[maxl];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline void calc(int id) {
	for(int i = 1; i <= m; i++) hash[id][i] = hash[id][i - 1] * 179 + str[i];
	for(int i = m; i >= 1; i--) hsah[id][i] = hsah[id][i + 1] * 971 + str[i];
}

int main() {
	n = iread(); m = iread(); s = iread();
	for(int i = 1; i <= n; i++) {
		scanf("%s", str + 1);
		calc(i);
	}
	int ans = 0;
	for(int i = 1; i <= m; i++) {
		for(int j = 1; j <= n; j++) tmp[j] = hash[j][i - 1] * 769 + hsah[j][i + 1] * 967;
		sort(tmp + 1, tmp + 1 + n);
		int cnt = 1;
		for(int j = 2; j <= n; j++)
			if(tmp[j - 1] == tmp[j]) ans += cnt, cnt++;
			else cnt = 1;
	}
	printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(hash)