LA 3942 && UVa 1401 Remember the Word (Trie + DP)

Source: Internet
Author: User

Test instructions: give you a dictionary with a different word of s and a long string l, so that you can break the long string into several words (the word is reusable) and how many. (Algorithm starter Training guide-p209)

Analysis: I go, a look at this is not a DP it? Just started to pay has been runtime error, looking for a long time, always thought is the array opened small, constantly increasing or so, later found that I used the char type ... The following analysis of the topic

It should not be difficult to think of this state transfer equation:

D (i) = Sum{d (I+len (x)) | Word X is s[i ... L] of the prefix}, where Len (x) is the length. D (i) represents a string starting from character I (that is, suffix s[i ...). L]) Number of species.

Obviously we are pushing forward from the back, and the D (i) species should be made up of D (i) and D (I+len (x)) and composed (think why, do not understand can draw a diagram analysis).

If you enumerate x first, then judge if it is s[i ... L] prefix, the time complexity is too high. So change a train of thought, first make the word tire (prefix tree), then try to find s[i in tire. L]. Specific reference code.

The code is as follows:

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intMAXN =4000* -+Ten;//4,000 words, each word the longest is 100, at most there are so manyConst intMAXM =300010;Const intMoD =20071027;intD[MAXM];CharSS[MAXM], t[ the];structtire{intch[maxn][ -]; intVAL[MAXN]; intsz; voidInit () {sz =1; Memset (Val,0,sizeof(Val)); memset (ch[0],0,sizeof(ch[0])); }//Initialize    intIdxCharc) {returnA O'a'; }//Get number    voidInser (Char*s) {//Insert        intU =0, n =strlen (s);  for(inti =0; I < n; ++i) {            intc =idx (s[i]); if(!Ch[u][c]) {memset (Ch[sz],0,sizeof(Ch[sz])); CH[U][C]= sz++; } u=Ch[u][c]; } Val[u]=N; }    voidQuary (Char*s,intIintN) {//Find        intU =0;  for(intj =0; J < N; ++j) {            intc =idx (s[j]); if(!ch[u][c])return ; U=Ch[u][c]; if(Val[u]) d[i] = (D[i] + d[i + val[u])%MoD; }    }}; Tire Tire;intMain () {intN, Kase =0;  while(~SCANF ("%s%d", SS, &N)) {tire.init ();//Be sure to initialize, just start tire definition in the inside, a run on the collapse ... I'm drunk, too.                 for(inti =0; I < n; ++i) {scanf ("%s", T);        Tire.inser (t); } memset (d,0,sizeof(d)); intLen =strlen (ss); D[len]=1;//This place is the boundary, notice the initialization         for(inti = len-1; I >=0; --i) tire.quary (SS+i, I, len-i);//Recursive seed Countprintf"Case %d:%d\n", ++kase, d[0]); }    return 0;}

LA 3942 && UVa 1401 Remember the Word (Trie + DP)

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.