HDU 3336 Count The string (next array utilization)

Source: Internet
Author: User


Count the string Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 5224 Accepted Submission (s): 2454
problem DescriptionIt is the well known this aekdycoin is good at the 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 is: "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 is asked to calculate the sum of the match times for all the prefixes. For "Abab", it is 2 + 2 + 1 + 1 = 6.
The answer is very large, so output the answer mod 10007.
 
InputThe first line was a single integer T, indicating the number of test cases.
For each case, the first line was an integer n (1 <= n <= 200000), and which is the length of the string s. A line follows giving the string s. The characters in the strings is all lower-case letters.
 
Outputfor each case, output is only one number:the sum of the match times for all the prefixes of S mod 10007. 
Sample Input
14abab
 
Sample Output
6
 
Author[email protected] 
SourceHdoj Monthly contest–2010.03.06

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3336

To give a string, the number of matches of each prefix substring in the original string

Title Analysis: Good problem is the data is too water, 1 5 Ababa is obviously 9, but a lot of running out of 8 of the program are AC, I say the next positive solution, to understand the meaning of next array, The value of the next array is the number of suffixes of the current substring matching the prefix, we first initialize the Ans=len, indicating that each prefix appears only once, and then according to the next array, find a matching suffix ans+1, followed by the next back to 0, It is equivalent to the prefix substring contained in a string, for example:
s a b a B a
I 0 1 2 3 4 5
Next[i]-1 0 0 1 2 3

Start ans=5
NEXT[3] = 1,ans + 1 = 6,next[1] = 0
NEXT[4] = 2,ans + 1 = 7,next[2] = 0
NEXT[5] = 3,ans + 1 = 8,next[3] = 1,ans + 1 = 9 (this means that the ABA suffix contains a prefix a)
So the final answer is 9.

#include <cstdio> #include <cstring>int const MAX = 1e8;int Const MOD = 10007;int Next[max];char s[max];void ge T_next () {    int i = 0, j =-1;    Next[0] =-1;    while (s[i]! = ' + ') {        if (j = =-1 | | s[i] = = S[j])        {            i++;            j + +;            Next[i] = j;        }        else            j = next[j];    }} int main () {    int T, Len, ans;    scanf ("%d", &t);    while (t--)    {        memset (next, 0, sizeof (next));        scanf ("%d%s", &len, s);        Get_next ();        ans = len;        for (int i = 1; I <= len; i++)        {            int tmp = next[i];            while (TMP)            {                ans = (ans + 1)% MOD;                TMP = next[tmp];            }        }        printf ("%d\n", ans);}    }




HDU 3336 Count The string (next array utilization)

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.