bzoj3555 hash

题意:给n个字符串,保证两两不重复。现在定义一对字符串如果仅只有1位不同,那么它视为相似字符串,问现在有多少对相似字符串。
思路:题目没给数据,其实可以O(lnlogn)暴力的。用hash爆力处理。
这次一开始用的map,TLE,后来用unordered_map,ce,最后用了数组。记录下,证明map相对慢一些。
代码:

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long 
#define for1(i,n) for(int i=1;i<=n;i++)
#define forn(i,n) for(int i=0;i<n;i++)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxn = 3e4+5;
const int seed = 13331;
string s[maxn];
ull S[maxn],P[205];

int main(){
    IO;
    P[0] = 1;
    for1(i,204) P[i] = P[i-1]*seed;
    int n,len,x;cin>>n>>len>>x;
    forn(i,n){
        cin>>s[i];
        for1(j,len) S[i] = S[i]*seed+s[i][j-1];
    }
    int ans = 0;
    for1(i,len){
        vector<ull> a;
        forn(j,n){
            ull x = S[j]-s[j][i-1]*P[len-i];
            a.push_back(x);
        }
        sort(a.begin(),a.end());
        int cnt = 0;
        forn(i,a.size()-1){
            //cerr<<a[i]<<' '<<cnt<<'\n';
            if(a[i]==a[i+1]) cnt++;
            else{
                ans+=(1+cnt)*cnt/2;
                cnt = 0;
            }
        }
        ans+=(1+cnt)*cnt/2;
        cnt = 0;
        //cerr<<'\n';
        // for(auto x:mp)if(x.second>1){
        //    // cerr<<i<<' '<<x.second<<'\n';
        //     ans+=(1+x.second-1)*(x.second-1)/2;
        // }
    }
    cout<<ans<<'\n';
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值