省选专练[CTSC2014]企鹅QQ

我不会Hash

学习了一下

hash就是暴力乘模数

比如19260817什么的

然后前后Hash+自然溢

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
#define LL unsigned long long 
const int N=3e4+100;
const int maxn=251;
LL St[N][maxn]={};
LL Ed[N][maxn]={};
LL tmp[N]={};
char ch[maxn]={};
int n,m,s;
int Prim[4]={19260817,233,419,231};
void Calc(int idx){
//	St[idx][0]=1;
	for(int i=1;i<=m;i++){
		St[idx][i]=(St[idx][i-1]*Prim[0]+(int)ch[i]);	
//		cout<<St[idx][i]<<" ";
	}
//	Ed[idx][m]=1;
	for(int i=m;i>=1;i--){
		Ed[idx][i]=(Ed[idx][i+1]*Prim[1]+(int)ch[i]);
//		cout<<(int)ch[i]<<" "<<Ed[idx][i]<<" - ";
	}
	/*for(int i=1;i<=m;i++){
	}*/
	/*for(int i=1;i<=m;i++){
		cout<<St[idx][i]<<" ";
	}
	cout<<'\n';
	for(int i=1;i<=m;i++){
		cout<<Ed[idx][i]<<" ";
	}
	cout<<'\n';*/
}
int ans=0;
int main(){
//	freopen("2947.in","r",stdin);
//	cout<<Prim[0]<<" "<<Prim[1]<<" "<<Prim[2]<<" "<<Prim[3]<<'\n';
	scanf("%d%d%d",&n,&m,&s);
	for(int i=1;i<=n;i++){
		scanf("%s",ch+1);
		/*for(int j=1;j<=m;j++){
			cout<<(int)(ch[j])<<" ";
		}*/
		Calc(i);
	}
//	cout<<"000000---90"<<'\n';
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			tmp[j]=St[j][i-1]*Prim[2]+Ed[j][i+1]*Prim[3];
//			cout<<St[j][i-1]<<" "<<Ed[j][i+1]<<'\n';
		}
		sort(tmp+1,tmp+1+n);
		
		int now=1;
		/*for(int j=1;j<=n;j++){
			cout<<tmp[j]<<" ";
		}*/
		for(int j=2;j<=n;j++){
//			cout<<tmp[j]<<" ";
			if(tmp[j]==tmp[j-1])ans+=now,now++;
			else now=1;
		}
//		cout<<'\n';
	}
	cout<<ans;
}

猜你喜欢

转载自blog.csdn.net/fcb_x/article/details/81045856