Match statistics

Match statistics

Given two strings \ (\ {a_i \}, \ {B_i \} \) , each of length \ (n-, m \) , there is a query q, each time a query string x of length exactly in the number of locations, to ensure that the location is the beginning of the prefix \ (\ {b_i \} \ ) matches exactly the length \ (X, 1 ≦ n, m, Q, x≤200000 \) .

solution

Matching problem, consider kmp, may wish to use kmp, to obtain a string array of f to determine the next string array b, followed by metaphysics operation, I can not reflect the thinking process.

First, note a \ (CNT [i] \) represents the length of at least the number i of the position satisfies the meaning of the title, as long as each query to obtain the x, we just answer \ (cnt [x] -cnt [ x + 1] \) , natural to think of every i, have come to a \ (++ cnt [f [i]] \) , but this is problematic.

We must first understand the nature of the match, otherwise incomprehensible, that is, \ (F_i \) , to have part of the value (that is, non-zero) must be continuous section by section, and according kmp nature 3, the position j, readily known fact \ (a [j + i + 1 \ sim j] \) is capable of \ (b [1 \ sim i ] \) same proviso \ (a [j + i + 1 \ sim j-1] \) and \ (b [1 \ sim i -1] \) the same, then it is determined \ (b [i] \) and \ (a [j] \) are the same.

If you really understand the above words (do not know what I'm saying, why have these things, Duokanjibian, multi-drawing), easy to know every time we \ (++ cnt [f [i ]] \) (we are beginning a required prefix to a location), then there must be a prefix for a starting position, it's up to the maximum length of 0 will be counted once, this was in line with our \ (cnt [i] \) of meaning, and apparently this is considered less, because \ (F [i] \) the meaning of the string ends with a suffix i, b can be the maximum length of the matching string, it is in fact for a string \ (IF [i] +1 \) position, the position may also be allowed to exist with the suffix b matches a string beginning with it.

The nature kmp 1, easy to know for each \ (F [i] \) , the match length is exactly these locations \ (Next [F [i]] \) , so we expressed at least as long as i is greater than the reverse enumeration length i equal to the number of the request, each time an operation \ (CNT [Next [i]] + = CNT [i] \) , and this recurrence equation reason is that no aftereffect \ ([i] Next \) must be smaller than \ (i \) , the meaning of which is equal to a length greater than the number of positions i, which have a length \ (next [i] \) contributes, in fact, well understood, because the length of suboptimal decisions of kmp sure i point is \ (the Next [i] \) , and larger than i length j, kmp nature tells us that j is the basis of the i's on, must meet suboptimal decision point \ (next [j], next [next [ j]] ... \) down certainly equal to i, and apparently \ (cnt [i] \) there will include these circumstances, and in any event these cases are larger than \ ([i the Next] \) , which is equal to \ (next [i] \) of a portion has been calculated before \ (+ \) of the time has been calculated.

This question is very abstract, more emotional part appreciated, the last time complexity can be done \ (O (n-) \) , the code is very simple.

Reference Code:

#include <iostream>
#include <cstdio>
#define il inline
#define ri register
#define Size 200500
using namespace std;
char a[Size],b[Size];
int Next[Size],f[Size],cnt[Size];
il void get(char&),
    kmp(char[],int,char[],int);
int main(){
    int n,m,q;
    scanf("%d%d%d",&n,&m,&q);
    for(int i(1);i<=n;++i)get(a[i]);
    for(int i(1);i<=m;++i)get(b[i]);
    kmp(b,m,a,n);for(int i(1);i<=n;++i)++cnt[f[i]];
    for(int i(m);i;--i)cnt[Next[i]]+=cnt[i];
    while(q--)scanf("%d",&n),printf("%d\n",cnt[n]-cnt[n+1]);
    return 0;
}
il void kmp(char a[],int la,char b[],int lb){
    for(int i(2),j(0);i<=la;++i){
        while(j&&a[j+1]!=a[i])j=Next[j];
        if(a[j+1]==a[i])++j;Next[i]=j;
    }for(int i(1),j(0);i<=lb;++i){
        while(j&&(a[j+1]!=b[i]||j==la))j=Next[j];
        if(a[j+1]==b[i])++j;f[i]=j;
    }
}
il void get(char &c){
    while(c=getchar(),c==' '||c=='\n'||c=='\r');
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/11257792.html