Count the string (hdu3336)

Source: Internet
Author: User

Count the string

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 7190 Accepted Submission (s): 3318


Problem DescriptionIt is well known the 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 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 only one number:the sum of the match times for all the prefixes of S mod 10007.

Sample input14ababsample Output idea: KMP (next array) +dp;dp[i]= (dp[next2[i]]+1)%mod;dp transfer equation, which indicates that all new prefixes with [1,i] are added at the end of I. Aaaaadp[1]=1;dp[2]=2 (indicates new a AA); Dp[3]=3 (aaa,aa,a) ... dp[i]= (dp[next2[i]]+1) is the reason for example is Abaab, assuming that the previous is finished, now add a letter, abaaba,next[i]=3; So the new and for an ABA meet the requirements, that is dp[3];
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <stdlib.h>6#include <queue>7#include <cstdio>8#include <math.h>9 voidNEXT1 (intk);Ten Charstr[300000]; One intNEXT2 [300000]; A Charstrr[300000]; - intdp[300000]; - Const intMod=10007; the using namespacestd; - intMainvoid) - { -     inti,j,k,p,q; +scanf"%d",&k); -      while(k--) +     { A         intsum=0; atscanf"%d",&p); -scanf"%s", str); -         intL=strlen (str); -          for(i=0; i<l; i++) -{strr[i+1]=str[i];} - Next1 (l); in          for(i=1; i<=l; i++) -         { toDp[i]= (dp[next2[i]]+1)%MoD; +Sum= (Sum+dp[i])%MoD; -         } theprintf"%d\n", sum); *     } $     return 0;Panax Notoginseng } -  the voidNEXT1 (intk) + { A     inti,j; thenext2[0]=0; +next2[1]=0; -j=0;intCnt=0; $      for(i=2; i<=k; i++) $     { -          while(j>0&&strr[j+1]!=Strr[i]) -         { thej=Next2[j]; -         }Wuyi         if(strr[j+1]==Strr[i]) the         { -J + +; Wu         } -next2[i]=j;cnt++; About     } $}

Count the string (hdu3336)

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.