【BZOJ】【P3555】【Ctsc2014】【企鹅QQ】【题解】【hash】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3555

史上最水的ctsc题……

枚举不同的位置,hash判断

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<cctype>
using namespace std;
typedef unsigned long long UL;
const int maxn=30010;
UL hash[maxn][202];
UL base=233;
UL hash_l[202];
int n,len;
char s[202];
UL ans=0;
void hash_init(int x){
	UL val=0;
	for(int i=0;i<len;i++)
	val=val*base+s[i],
	hash[x][i]=val;
}
 UL GetHash(int x,int l,int r){
	if(l>r)return 0;
	return hash[x][r]-(l?hash[x][l-1]:0)*hash_l[r-l+1];	
}
pair<UL,UL> H[maxn];
int Hsize;
int main(){
	scanf("%d%d%*d",&n,&len);
	hash_l[0]=1;
	for(int i=1;i<=len;i++)hash_l[i]=hash_l[i-1]*base;
	for(int i=1;i<=n;i++){
		scanf("%s",s);
		hash_init(i);
	}
	for(int i=0;i<len;i++){
		Hsize=0;
		for(int j=1;j<=n;j++)
		H[++Hsize]=make_pair(GetHash(j,0,i-1),GetHash(j,i+1,len-1));
		sort(H+1,H+1+Hsize);
		for(int i=1;i<n;i++){
			int cnt=1;
			while(i!=n&&H[i]==H[i+1])cnt++,i++;
			ans+=(UL)(cnt-1)*cnt/2;
		}
	}cout<<ans<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值