标签:srand def inline rand stream main style cst sync
字符串哈希
#include<cstdio> #include<algorithm> #include<vector> #include<ctime> #include<string> #include<cstdlib> #include<iostream> #include<map> #include<unordered_map> typedef unsigned long long u64; int n,l,s; u64 map1[256]; u64 map2[256]; inline u64 rd(){return(u64)rand()<<46^(u64)rand()<<35^(u64)rand()<<24^rand();} std::string str; std::vector<u64>v; int main(){ srand(time(0)); std::ios::sync_with_stdio(false),std::cin.tie(0); std::cin >> n >> l >> s; for(int i=0;i<256;++i){ map1[i]=rd(); map2[i]=rd(); } u64 ans=0; for(int i=1;i<=n;++i){ std::cin >> str; u64 hsh=0; for(int i=0;i<l;++i)hsh^=map1[i]*map2[str[i]]; for(int i=0;i<l;++i)v.push_back(hsh^map1[i]*map2[str[i]]); } std::sort(v.begin(),v.end()); for(std::vector<u64>::iterator l=v.begin(),r=v.begin();l!=v.end();){ r=l; while(r!=v.end() && *l == *r)++r; --r; ans+=(u64)(r-l+1)*(r-l)/2,l=r+1; } std::cout << ans << ‘\n‘; }
标签:srand def inline rand stream main style cst sync
原文地址:https://www.cnblogs.com/skip1978/p/10348127.html