[BZOJ3555][Ctsc2014]企鹅QQ(hash)

11 篇文章 0 订阅

题目描述

传送门

题解

枚举每一位,把这一位去掉,hash统计有多少个一样的
因为不存在两个串相同,所以不用管其它的直接搞就行
sort要快很多,map跑好慢…

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
#define UL unsigned long long

const UL p=2000001001LL;
int n,l,ans;
char s[30005][205];
UL hash[30005],mi[205];
map <UL,int> Cnt;

int main()
{
    int lalala;
    scanf("%d%d%d",&n,&l,&lalala);
    for (int i=1;i<=n;++i) scanf("%s",s[i]);
    mi[0]=1LL;for (int i=1;i<=l;++i) mi[i]=mi[i-1]*p;
    for (int i=1;i<=n;++i)
        for (int j=0;j<l;++j) hash[i]+=(UL)s[i][j]*mi[l-j-1];
    for (int j=0;j<l;++j)
    {
        Cnt.clear();
        for (int i=1;i<=n;++i)
        {
            hash[i]-=(UL)s[i][j]*mi[l-j-1];
            if (!Cnt[hash[i]]) ++Cnt[hash[i]];
            else
            {
                int cnt=Cnt[hash[i]];
                ans-=cnt*(cnt-1)/2;
                ++cnt;
                ans+=cnt*(cnt-1)/2;
                Cnt[hash[i]]=cnt;
            }
            hash[i]+=(UL)s[i][j]*mi[l-j-1];
        }
    }
    printf("%d\n",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值