Bzoj 3555: [Ctsc2014] Penguin QQ [string hash] "Learning note"

Source: Internet
Author: User

3555: [Ctsc2014] Penguin qq time limit:20 Sec Memory limit:256 MB
submit:2046 solved:749
[Submit] [Status] [Discuss] Description

PENGUINQQ is China's largest and most influential SNS (social Networking Services) website, based on the real name, to provide users with logs, groups, instant messaging, photo albums, markets and other rich and powerful Internet functionality experience, to meet users of social, information, entertainment, Transactions and other aspects of demand.
Little Q is the administrator of the PENGUINQQ website, who recently conducted an interesting study-which accounts are registered with the same person. After a long period of analysis, small q found that the same person registered account name is always very similar, such as penguin1,penguin2,penguin3 ... So the small Q decided to make a statistical analysis of this similar situation first.
A small Q definition, if two account names are similar, when and only if the two strings are equal and only one bit different. For example "Penguin1" and "Penguin2" are similar, but "Penguin1" and "2Penguin" are not similar. And little Q wants to know how many pairs are similar in a given account name.
To simplify your work, the string length that small q gives you is equal to and contains only uppercase and lowercase letters, numbers, underscores, and ' @ ' a total of 64 characters, and there are no two identical account names.

Input

The first line contains three positive integers,. It represents the number of account names, indicating the length of the account name, used to indicate the size of the character set, and its value can only be 2 or 64.
If equal to 2, the account name contains only the characters ' 0 ' and ' 1 ' of 2 characters;
If equal to 64, the account name may contain uppercase and lowercase letters, numbers, underscores, and ' @ ' a total of 64 characters.
Subsequent lines, one string of length per line, are used to describe an account name. The data guarantees that a string is 22 different.

Output

A single positive integer that indicates how many pairs of similar account names are in common.

Sample Input4 3 64
Fax
Fax
Max
MacSample Output4HINT

4 pairs of similar strings are: Fax with Fax,fax and Max,fax with Max,max and Mac. N<=30000,l<=200,s<=64

L only 200, enumerate which one is different, and then sort the hash values.

Problem:

1. How to process the hash value each time:

Let the hash value minus the bit J of the enumeration, there are two methods, you can only process the prefix hash and then-ha[i][j]*po[m-j]+ha[i][j-1]*po[m-j+1], or handle the prefix and suffix hash and then J on both sides of the combination.

PS: How to quickly find the hash value of a substring?

Preprocessing the hash value of all prefixes of B val[i].
It is assumed that the C-system is used to preprocess c^0, c^1 、......、 c^m.
So
b[x,y]=val[y]-val[x-1]*c^ (y-x+1)

2. How to count the same number after sorting:

This simple question ..... Maintain a CNT for the current number of consecutive same ...

Note: strings are not just 64,ascall yards to more than 90 .... Make it larger and preferably prime

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;typedef unsignedLong Longll;Const intn=3e4+5, m=205, b=107; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intN,m,ans;ll ha[n][m],a[n],po[m];CharS[m];intMain () {//freopen ("In.txt", "R", stdin);N=read (); M=read ();int_=read (); po[0]=1;  for(intI=1; i<=m;i++) po[i]=b*po[i-1];  for(intI=1; i<=n;i++) {scanf ("%s", s+1);  for(intj=1; j<=m;j++) ha[i][j]=ha[i][j-1]*b+S[j]; }     for(intj=1; j<=m;j++){         for(intI=1; i<=n;i++) a[i]=ha[i][m]-ha[i][j]*po[m-j]+ha[i][j-1]*po[m-j+1]; Sort (a+1, A +1+N); intCnt=0;  for(intI=2; i<=n;i++){            if(a[i]!=a[i-1]) cnt=0; Elsecnt++; Ans+=CNT; }} printf ("%d", ans);}

Bzoj 3555: [Ctsc2014] Penguin QQ [string hash] "Learning note"

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.