Count the string
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3132 accepted submission (s): 1458
Problem descriptionit 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.
Inputthe 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.
Outputfor each case, output only one number: The sum of the match times for all the prefixes of S mod 10007.
Sample Input
14abab
Sample output
6
I will give you a string, count the number of times all the prefixes of this string appear in the string, and then model 100007. Solution: At first glance, I really don't know how to analyze it. Break down the problem into the prefix of the substring ending with a [I], and then DP to obtain it. For details about the specific ideas, see the code.
Address: Count the string
AC code:
/* Next [J] = I, DP [J] = DP [I] + 1; the last letter prefix with I contains DP [I, the last letter prefix with J contains DP [I] + 1, and one is added. the following is an example: a B c a B c d next [I] 0 0 1 2 0 1 2 3 4 5 0 DP [I] 1 1 2 2 1 2 2 3 3 2 1 Set DP [I]: total number of substrings ending with a [I] With a prefix */# include <iostream> # include <cstring> # include <string> # include <cstdio> # define Mo 10007 using namespace STD; int Len, next [200005], DP [200005], Res; char a [200005]; void getnext () {int I, j; next [0] = 0, next [1] = 0; for (I = 1; I <Len; I ++) {J = next [I]; while (J & A [I]! = A [J]) J = next [J]; if (a [I] = A [J]) next [I + 1] = J + 1; else next [I + 1] = 0 ;}} int main () {int T, I; scanf ("% d", & T); While (t --) {scanf ("% d % s", & Len, a); getnext (); memset (DP, 0, sizeof (DP); Res = 0; for (I = 1; I <= Len; I ++) {DP [I] = DP [next [I] + 1; res = (RES + dp [I]) % Mo;} cout <res <Endl;} return 0;}/* 311abcababcd */