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<<'
';
            if(a[i]==a[i+1]) cnt++;
            else{
                ans+=(1+cnt)*cnt/2;
                cnt = 0;
            }
        }
        ans+=(1+cnt)*cnt/2;
        cnt = 0;
        //cerr<<'
';
        // for(auto x:mp)if(x.second>1){
        //    // cerr<<i<<' '<<x.second<<'
';
        //     ans+=(1+x.second-1)*(x.second-1)/2;
        // }
    }
    cout<<ans<<'
';
    return 0;
}
人一我百,人十我万。
原文地址:https://www.cnblogs.com/AlexPanda/p/12520308.html