3.3 Strings (1)

Source: Internet
Author: User
Tags cas

Trie Prefix Tree Dictionary tree

Examples of LA 3942 http://acm.hust.edu.cn/vjudge/problem/22109

The first example of a dictionary tree I use set water over, first record this version, test instructions is, give a string, there are some short strings of the collection, ask how many different splitting methods so that after splitting, each substring in the collection.

Dp[len] Indicates how many split methods before Len Dp[0]=1, the first 0 characters have a split method, empty. Dp[len] is the answer, the transfer is, if the next paragraph in the collection, then you can transfer to length plus the length of the paragraph.

Starting point has la=3e5,, to each starting point, add the string may have lbi=100, all to enumerate, next is to determine whether the string exists, with hash value, O1,set query Logn, n=4000, the overall complexity la*lb*logn drifting over.

1 //#define Txtout2 //#define DEBUG3#include <bits/stdc++.h>4 #defineMT (A, b) memset (A,b,sizeof (a))5 using namespacestd;6typedefLong LongLL;7 Const DoublePi=acos (-1.0);8 Const Doubleeps=1e-8;9 Const intinf=0x3f3f3f3f;Ten Const intm=3e5+Ten; One ConstLL mod=20071027; A classString_hash {///string Hash init o (n) query O (1) -typedef unsignedLong LongTypec;///hash value type -     Static Const intmv=3e5+Ten;///string Length the     Static Const intkey=137;///good choice of prime number of hash seed value - Typec H[MV],XP[MV]; -  Public: -     voidInitCharS[],intLS) {///incoming string array and string length +h[ls]=0; -          for(inti=ls-1; i>=0; i--) { +h[i]=h[i+1]*key+S[i]; A         } atxp[0]=1; -          for(intI=1; i<=ls; i++) { -xp[i]=xp[i-1]*key; -         } -     } -TypecGet(intPosintLen) {///incoming initial position pos, string length len, return string hash value in         returnh[pos]-h[pos+len]*Xp[len]; -     } to }hash; + intN; - CharA[m]; the Charb[4010][ the]; * LL dp[m]; $ Set<unsignedLong Long>s;Panax Notoginseng intMax_len; - voidInit_set () { the s.clear (); +max_len=0; A      for(intI=0; i<n; i++) { the         intlb=strlen (B[i]); + Hash.init (b[i],lb); -S.insert (Hash.Get(0, LB)); $max_len=Max (max_len,lb); $     } - } - LL Solve () { the Init_set (); -     intlen=strlen (a);Wuyi      for(intI=0; i<=len; i++) { thedp[i]=0; -     } Wudp[0]=1; - Hash.init (A,strlen (a)); About      for(intI=0; i<len; i++) { $         if(dp[i]==0)Continue; -          for(intj=0; j<max_len; J + +) { -             if(!s.count (Hash).Get(i,j+1)))Continue; -dp[i+j+1]+=Dp[i]; Adp[i+j+1]%=MOD; +         } the     } -     returnDp[len]; $ } the intMain () { the #ifdef txtout theFreopen ("In.txt","R", stdin); theFreopen ("OUT.txt","W", stdout); - #endif //Txtout in     intcas=1; the      while(~SCANF ("%s%d",a,&N)) { the          for(intI=0; i<n; i++) { Aboutscanf"%s", B[i]); the         } theprintf"Case %d:%lld\n", cas++, Solve ()); the     } +     return 0; -}
View Code


3.3 Strings (1)

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.