Count the string (used in next array KMP)

Count the string (used in next array KMP)

topic

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
Input
The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
Sample Input
1
4
abab
Sample Output
6

The meaning of problems

Given a string, obtains the number of all prefixes appear in the string, the prefix itself can be considered.

Thinking

Read the solution to a problem
first of all, say the title is next array to use, then say this: Sometimes, in order to search for other string KMP, when calculating the next array will to speed up the search, a small array of next optimization.
Below :

//优化前
while (j < len)
    {
        if (i == -1 || str[i] == str[j])  
            nex[++j] = ++i;
        else
            i = nex[i];
    }
}
//====================================
//优化后
while (j < len)
    {
        if (i == -1 || str[i] == str[j])
        {
            if (str[++i] == str[++j])
                nex[j] = nex[i];
            else
                nex[j] = i;
        }
        else
            i = nex[i];
    }
}

But sometimes there is no optimization will be used after the next array, such as the title.
Topic solution, create a relationship with an array Next, Next time will use an array of queries back, which proves that you are looking for strings, there have been before,
so that you can be calculated based on the number of occurrences of backtracking.
For example, the title of

序号    0  1  2  3  4

字符串     a  b  a  b

next   -1  0  0  1  2

Starting from j = 1, 1 a back sum + = 1, j = 2 is the first time, sum + = 1, j = 3 and j = 4, respectively back twice, sum + = 2, sum + = 2.
Therefore, a total of six times. ( Reprint )
here to explain:
for example, serial number 4 b. 2 back to the first time, then to 0, and the, sum + = 2. The first plus is the "ab" is. The second plus is "abab" is.
Do not forget to modulo.

answer

#include <iostream>
#include <cstring>
#define N 200077
using namespace std;

char str[N];
int nex[N];
int len;
void getNext()
{
    int i = -1, j = 0;
    nex[0] = -1;
    while (j < len)
    {
        if (i == -1 || str[i] == str[j])
            nex[++j] = ++i;
        else
            i = nex[i];
    }
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> len;
        cin >> str;
        getNext();
        int res = 0;
        for (int i = 1; i <= len; i++)
        {
            int p = nex[i];
            while (p!=-1)
            {
                res++;
                p = nex[p];
            }
        }
        cout<<res%10007<<endl;
    }
}

Guess you like

Origin www.cnblogs.com/tttfu/p/11307961.html