HDU 3336 count the string (memory-based search)

Source: Internet
Author: User
Count the string

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 745 accepted submission (s): 320


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
1
4
abab
 

 

Sample output
6

Solution:

With the use of memory-based search, the algorithm is still a search process, but some of the searched results are saved using the idea and pattern of dynamic planning. The memory algorithm still follows the top-down order when solving the problem. However, every time a State is solved, the solution is saved. When this state is encountered again, you don't have to solve it again.
The question means that to give a string, you must overlay it from the first character. The total number of times it matches the original string. The array length is too long and time-out is easy, so the algorithm complexity is reduced. We give an integer B [] array record matching subscript (starting from 1 subscript ). As shown in:

From the above figure, we can know that the values of the B [] array obtained each time are useful for subsequent solutions and are necessary conditions, which can save a lot of time.

# Include "iostream" <br/> using namespace STD; <br/> # define maxlength 200002 <br/> char s [maxlength]; <br/> int B [maxlength]; <br/> int main () <br/> {<br/> int t, n, I, J, K, Q, l; <br/> CIN> T; <br/> while (t --) <br/>{< br/> CIN> N> S; k = 0; q = 0; <br/> // memset (B, '/0', sizeof (B); <br/> for (I = 0; I <n; I ++) <br/> {<br/> If (s [I] = s [0]) <br/>{< br/> B [k ++] = I + 1; q ++; <br/>}< br/> q %= 10007; <br/> for (I = 1; I <n; I ++) <br/> {<br/> L = K; k = 0; <br/> for (j = 0; j <L; j ++) <br/> {<br/> If (s [I] = s [B [J]) <br/> {B [k ++] = B [J] + 1; q ++ ;} <br/>}< br/> B [k] = '/0'; q %= 10007; <br/>}< br/> cout <q % 10007 <Endl; <br/>}< br/> return 0; <br/>}

 

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.