"String Hash" bzoj3555 [Ctsc2014] Penguin QQ

Source: Internet
Author: User

Enumerate each location, give the first half of each string a hash value, a hash value for the second half, and, if they are equal, the two strings are similar.

After each transfer, sort it out.

O (L*n*log (n)).

#include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace Std;typedef unsigned long long ull;struct hash{ull l,r;} Hss[30001],tmp[30001];bool operator < (const hash &AMP;A,CONST hash &b) {return a.l!=b.l? A.L&LT;B.L:A.R&LT;B.R;} BOOL operator! = (const hash &AMP;A,CONST hash &b) {return (a.l!=b.l| | A.R!=B.R);} ull seed,seeds[201];int Ord[301],n,m,ans;char s[30001][201];void init () {if (seed==2) ord[' 0 ']=1,ord[' 1 ']=2;else {int  en=0;  for (char c= ' A '; c<= ' Z '; ++c) Ord[c]=++en;  for (Char c= ' a '; c<= ' z '; ++c) Ord[c]=++en;  for (char c= ' 0 '; c<= ' 9 '; ++c) Ord[c]=++en; Ord[' _ ']=++en;  ord[' @ ']=++en; }++seed; seeds[0]=1;for (int i=1;i<=m;++i) seeds[i]=seeds[i-1]*seed;}  int main () {scanf ("%d%d", &n,&m), Cin>>seed;init (); for (int i=1;i<=n;++i) {scanf ("%s", S[i]);  for (int j=1;j<m;++j) hss[i].r=hss[i].r*seed+ (ull) ord[s[i][j];  }memcpy (TMP,HSS, (n+1) *sizeof (HASH)); for (int i=1;i<=m;++i) {int head; Sort (hss+1, hss+n+1);    for (int j=1;j<=n;++j) {tmp[j].l=tmp[j].l*seed+ (ull) ord[s[j][i-1]];    tmp[j].r-=seeds[m-1-i]* (ull) ord[s[j][i];    if (J==1 | | hss[j]!=hss[j-1]) head=j;    if (J==n | | hss[j]!=hss[j+1]) ans+= (((j-head+1) * (j-head)) >>1);  } memcpy (Hss,tmp, (n+1) *sizeof (HASH)); }printf ("%d\n", ans); return 0;}

String hash bzoj3555 [Ctsc2014] Penguin QQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.