Ultraviolet A 1401 DP + Trie

Source: Internet
Author: User
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & amp; Itemid = 8 & page = show_problem & amp; Category = & amp; problem = 4147

Question: Given a string and several words, there are several ways to use words to form a string. First, I had a problem with the DP equation. I don't know how to modify it for a long time, then, the array is too small to be replayed.
Trie array size = Number of words * Word Length
DP [I] is the extension of ANS starting with str [I], DP [I] = segma (DP [k]), where k Represents STR [I... k-1] is a word, if k = Len, then DP [I] ++;
# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; # define ll long longconst int n = 4000*100 + 100; const int mod = 20071027; char STR [300019], Pat [115]; ll DP [300019]; const int TK = 26, TB = 'a'; int top, tree [N] [TK + 1], Len; void Init () {Top = 1; memset (tree [0], 0, sizeof (tree [0]);} int sear (char * s, int I) {int CNT = 0; ll ans = 0; For (INT RT = 0; RT = tree [RT] [* s-TB]; ++ s) {If (* (S) = 0) break; CNT ++; If (Tree [RT] [TK]) // CNT! = Tree [RT] [TK] indicates DP [I .. len-1] is a word, at this time there is no increase in the number of splits {If (* (S + 1) = 0) ans ++; ans = (ANS + dp [I + CNT]) % MOD; ///// // printf ("RT = % d S = % s I = % d CNT = % d dp = % LLD \ n ", RT, STR + I + CNT, I, CNT, DP [I]); /// //} return ans;} void insert (char * s, int Rank = 0) // rank is the length of {int RT, NXT; For (RT = 0; * s; RT = NXT, ++ S, rank ++) {NXT = tree [RT] [* s-TB]; If (0 = NXT) // NXT! When it is set to 0, there is a public prefix. It has already been done before. You just need to continue the jump and insert "her" to "h" and "E" to "NXT! = 0 no need to insert {tree [RT] [* s-TB] = NXT = top; memset (tree [Top], 0, sizeof (tree [Top]); top ++ }}tree [RT] [TK] = rank;} int main () {// freopen ("uva1401.txt", "r", stdin); int N, ncase = 1, Pos; while (scanf ("% s", STR )! = EOF) {Init (); Pos = 0; Len = strlen (STR); scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% s", Pat); insert (PAT) ;}dp [Len] = 0; For (INT I = len-1; i> = 0; I --) {DP [I] = 0; DP [I] = sear (STR + I, I);} printf ("case % d: % LLD \ n ", ncase ++, DP [0]);} return 0 ;}



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.