bzoj_3555: [Ctsc2014]企鹅QQ

手动哈希

前缀和

比较字符串是否相同

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define ri register int
#define u unsigned long long
#define NN 30005
#define LL 205

using std::string;
using std::cout;
using std::endl;
using std::cin;

namespace mainstay {

    string s[NN];

    const u bas=87;

    u N,L,K,cnt,ans(0),t[NN],sum[NN][LL],val[NN]= {(u)1};

    inline void solve() {
        cin>>N>>L>>K;
        for(ri i(1); i<=N; ++i) cin>>s[i],s[i]='#'+s[i];
        for(ri i(1); i<=L; ++i) val[i]=bas*val[i-1];
        for(ri i(1); i<=N; ++i) {
            for(ri j(1); j<=L; ++j) {
                sum[i][j]=sum[i][j-1]+(s[i][j]-'0'+1)*val[j];
            }
        }
        for(u j(1); j<=L; ++j) {
            for(u i(1); i<=N; ++i) t[i]=sum[i][L]-(s[i][j]-'0'+1)*val[j];
            std::sort(t+1,t+N+1),cnt=1;
            for(ri i(2); i<=N+1; ++i) {
                if(t[i]==t[i-1]) ++cnt;
                else ans+=(cnt*(cnt-1)>>1),cnt=1;
            }
            if(cnt>1) ans+=(cnt*(cnt-1)>>1);
        }
        cout<<ans;
    }

}

int main() {

    //freopen("3555.in","r",stdin);
    //freopen("3555.out","w",stdout);
    std::ios::sync_with_stdio(false);
    mainstay::solve();

}

 

 

posted @ 2019-11-06 13:49  pai_hoo  阅读(85)  评论(0编辑  收藏  举报