UVALive-3942 (trie

Source: Internet
Author: User

Description

Neal is very curious on combinatorial problems, and now here comes a problem about words. Knowing that Ray had a photographic memory and this could not be trouble him, Neal gives it to Jiejie.

Since Jiejie can ' t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie's only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of St Icks.

The problem is as follows:a word needs to being divided into small pieces in such a-a-to, each piece are from some given s Et of words. Given a word and the set of words, Jiejie should calculate the number of ways the Given Word can be divided, using the WOR DS in the set.

Input

The input file contains multiple test cases. For each test case:the first line contains the given word whose length is no more than 300 000.

The second line contains an integer s , 1S4000.

Each of the following S lines contains one word from the set. Each word would be at the most characters long. There'll be no, identical words and all letters in the words would be lowercase.

There is a blank line between consecutive test cases.

You should proceed to the end of file.

Output

For each test case, the output of the number, as described above, from the task description modulo 20071027.

Sample Input

ABCD 4 a B cd AB

Sample Output

Case 1:2
Trie on the DP, the first write trie, found not to write the array version of Ah, than the pointer version of the slow many times ....
Const intmaxv=4e5+3;Constll mod=20071027;CharS[MAXV];intS;intIdxCharc) {returnC-'a';}structtrie{intch[maxv][ -]; BOOLMA[MAXV]; intsz; voidClear () {memset (ch,0,sizeofch); Memset (MA,0,sizeofMA); SZ=1; }    voidInsertChar*s) {        intn=strlen (s); intu=0, V;  for(intI=0; i<n;i++) {v=idx (s[i]); if(Ch[u][v]) u=Ch[u][v]; Else{Ch[u][v]=sz++; U=sz-1; }} Ma[u]=1; }}t;CharTMP[MAXV];Const intmaxdp=3e5+ -; ll DP[MAXDP];intMain () {    intt=0;  while(SCANF ("%s", s)! =EOF) {        intssz=strlen (s); T++; CIN>>S;        T.clear (); Memset (DP,0,sizeofDP);  while(s--) {scanf ("%s", TMP);        T.insert (TMP); } Dp[ssz]=1;  for(inti=ssz-1; i>=0; i--) {////Start match from last character            Char*su=&S[i]; intsusz=strlen (SU); intu=0;  for(intj=0; j<susz;j++){                intv=idx (su[j]); if(t.ch[u][v]&&T.ma[t.ch[u][v]]) {Dp[i]= (dp[i+j+1]+dp[i])%MoD; }                if(T.ch[u][v]) u=T.ch[u][v]; Else  Break; }} printf ("Case %d:%lld\n", t,dp[0]); }    return 0;}

UVALive-3942 (trie

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.