POJ 2406 Power strings--string hash (Bkdhash)

Source: Internet
Author: User

If the violence is resolved, the time complexity should be: O (n^2).

Takes a string hash with a time complexity of: O (N*LGN).

Using the next array of the KMP algorithm, the time complexity is: O (n).


I write with a string hash (Bkdhash), although not KMP, but still stick it:

#include <iostream>using namespace Std;typedef unsigned long long ull; Char arr[1000001];ull nbase[1000001];ull hash[1000001];int base = 31;void Main () {nbase[0] = 1;for (int i = 1; i < 10000 01; ++i) Nbase[i] = nbase[i-1] * Base;while (scanf_s ("%s", arr, 1000001)) {int len = strlen (arr); ull n = 0;  Hash[len] = 0;for (int i = len-1; I >= 0; i.) hash[i] = hash[i + 1] * base + arr[i]-' a ' + 1;for (int k = 1; k <= Len ++k) {if (len%k! = 0) Continue;ull temp = hash[0]-hash[k] * Nbase[k];int j = 0;for (j = k; J < len; j = j + k) {if (TEM P! = Hash[j]-hash[j + K] * nbase[k]) Break;else temp = hash[j]-hash[j + K] * nbase[k];} if (j = = len) {n = len/k;break;}} cout << N;}}


About bkdhash:http://blog.csdn.net/wanglx_/article/details/40400693

My own intuitive understanding:

To facilitate the explanation,

Assumption: base = 3, number range is 0-63 (the actual code is, base=31, the data type is unsigned long long)

For string ABCABC:

Index:0 1 2 3 4 5

Arr:a b c A b C

Hash: 34 11 3 (Calculation formula: hash[i]=hash[i+1]*base+arr[i]-' a ' + 1) There is a problem here: because the range of the number is 0-63 , the maximum is 63, there may be a number of overflow, such as:

From right to left, a->c this step, hash[2] = hash[3]*3+ ' C '-' a ' +1 =34*3+2+1= 105. 105 more than 63, do this?

The computer is automatically modulo (that is, take the remainder): 105% 64=41. All other hash values are calculated so that each step will take the remainder.

The same is true of Nbase's calculations: 3 9 (81%64) ...


So how does a computer judge the first three characters ("abc") to be equal to the last three characters ("abc")? Will the calculation be negative after taking the modulus?

After three characters: Temp0 = hash[3]-hash[6]*nbase[3] = 34-0*27 = 34

First three characters:temp1 = hash[0]-hash[3]*nbase[3] =-34*27 =?. Here 34*27 is actually (34*27)%64=22, so 56-22=34.

So temp0=temp1.


On the next array using the KMP algorithm, I thought:

(kmp:http://blog.csdn.net/v_july_v/article/details/7041827#comments)


I can't read it myself.

The code is as follows:

#include <iostream>using namespace Std;int getNext (char* arr, int len)         //Return last value: next[len-1]{int* next = new Int[len];memset (Next, 0, len*sizeof (int)), for (int i = 1; i < Len; ++i) {int p = next[i-1];while (p > 0 && Arr[i] = arr[p]) p = next[p-1];if (P = = 0 && arr[i]! = Arr[p])//when p = = 0 && arr[i]! = Arr[p], consider separately: next[ I] = 0. Next[i] = 0;else next[i] = p + 1;} return next[len-1];}  void Main () {char arr[1000001] = {'};while ' (scanf_s ("%s", arr, 1000001)) {int len = strlen (arr); int next = GetNext (arr, Len), if (len% (len-next) = = 0) cout << len/(len-next) << endl;elsecout << 1 << Endl;}}


POJ 2406 Power strings--string hash (Bkdhash)

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.