HASH [string], hashstring

Source: Internet
Author: User

HASH [string], hashstring

HASH is a common algorithm of OI.

The reason we commonly use hash is that hash can quickly (generally O (segment length) Find the hash value of a sub-segment, and then we can quickly determine whether the two strings are the same.

Today, we will talk about the hash of the string class.

 

It can be found that the HASH value related to a string is not only related to the number of each character, but also to the character's seat.

Through simple thinking, we can construct a model:

Write a comparisonNormalHash template.

Const  IntEE =97 ;
Const  IntMOD =100000007 ;
IntHASH (String &P )
{
IntE =1 ;
IntRet =0 ;
IntTl =P .Size ( ) ;
For  ( IntI =0 ;I <tl ;I + + )
Ret + =E *P [I ] ,E * =EE ;
ReturnRet ;
}
Question: KMP question description

For example, two strings s1 and s2 are given, where s2 is the Child string of s1, and all the positions where s2 appears in S1.

Input/Output Format

Input Format:

The first behavior is a string, that is, s1

The second behavior is a string, that is, s2

Output Format:

Several rows. Each row contains an integer indicating the position where s2 appears in s1.

The next line, including the length (s2) integers, indicates the value of the prefix array next [I.

Input and Output sample Input example #1:
ABABABCABA
Output sample #1:
13
Description

Time-Space limit: 1000 ms, 128 M

Data scale:

Set s1 length to N, s2 length to M

For 30% of data: N <= 15, M <= 5

For 70% of data: N <= 10000, M <= 100

For 100% of data: N <= 1000000, M <= 1000000

Ideas

Note:This question is resolved as KMP, not hash. If you want to know the KMP algorithm, please Baidu.

But what we learned is "hash". We cannot directly pre-process it. If we directly pre-process it, the time will be O (n * m) and it will blow up.

We can recursive:

"A [1] sequence with a known length of m... a [m], now known "a [1]... the hash value of a [m] "is K, and a [2]... hash Value of a [m + 1. "

You can think like this: "change the Assignment Method of EE and assign values in turn. In this way, you can directly Delete the first 'a [1] * EE ^ m-1 )', multiply by another 'ee ', move another digit back, and add a [m + 1]."

So the transfer equation is also easy to write, for HASH [I] = (HASH [I-1]-a [I-2] * E [1] % M + M) % M * EE % M + a [I-2 + m]; (HASH [I] indicates the hash value from a [I-1] to a [I + m-2.

Additional code:

# Include <cstdio>
# Include <cstring>
# Include <iostream>
# Include <algorithm>
Using  NamespaceStd ;
IntN ,K ,Len1 ,Len2 ;
IntNext1 [1000001 ] ;
CharS1 [1000001 ] ;
CharS2 [1000001 ] ;
Long  LongHASH [1000001 ] ;
Long  LongE [1000001 ] ,M =1234567898765 ;

Long  LongEE =97 ;

IntInit ( )
{
Long  LongKey =0 ;
IntAns =0 ;
Memset (E ,0 , Sizeof (E ) ) ;
Memset (HASH ,0 , Sizeof (HASH ) ) ;
E [Len2 ] =1 ;
For  ( IntI =Len2 -1 ;I > =1 ;I - - )
E [I ] =E [I +1 ] *EE %M ;
For  ( IntI =1 ;I < =Len2 ;I + + )
HASH [1 ] = (HASH [1 ] +E [I ] * (S1 [I -1 ] ) ) %M ;
For  ( IntI =1 ;I < =Len2 ;I + + )
Key = (Key +E [I ] * (S2 [I -1 ] ) ) %M ;
If  (HASH [1 ] = =Key )Ans + + ;
For  ( IntI =2 ;I < =Len1 -Len2 +1 ;I + + )
{
HASH [I ] = (HASH [I -1 ] -S1 [I -2 ] *E [1 ] %M +M ) %M *EE %M +S1 [I -2 +Len2 ] ;
If  (HASH [I ] = =Key )Ans + + ;
}
Printf ( "% D \ n" ,Ans ) ;
}
IntMain ( ) {
Scanf ( "% S" ,S1 )  ;
Scanf ( "% S" ,S2 )  ;
Len1 =Strlen (S1 ) ;
Len2 =Strlen (S2 ) ;
Init ( ) ;
Return0 ;
}

Related Article

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.