Bzoj 1212 L language (dp+ dictionary tree)

Source: Internet
Author: User

The longest prefix that can be understood.

It's obvious that DP. Make Dp[i]=true, which indicates that the prefix I can understand. Otherwise it cannot be understood. So dp[i+len]=dp[i]=true, when S[len] can match Str[i,i+len].

Because the pattern string length is 10. And the matching process can be accelerated with a dictionary tree.

So the complexity is O (10*m*len).

# include <cstdio># include<cstring># include<cstdlib># include<iostream># include<vector># include<queue># include<stack># include<map># include<Set># include<cmath># include<algorithm>using namespacestd;# define Lowbit (x) ( x)& (-x)) # define PI ACOs (-1.0) # define EPS 1e-9# define MOD1000000000# define INF1000000000# define MEM (A, B) memset (A, B,sizeof(a) # define for (I,a,n) for(intI=a; i<=n; ++i) # define FO (I,a,n) for(intI=a; i<n; ++i) # define bug puts ("H"); # define LCH P<<1, l,mid# define RCH p<<1|1, mid+1, r# define MP make_pair# define PB push_backtypedef pair<int,int>pii;typedef Vector<int>vi;# pragma comment (linker,"/stack:1024000000,1024000000") typedefLong LongLL;intScan () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}voidOut (inta) {if(a<0) {Putchar ('-'); a=-A;} if(a>=Ten) Out (A/Ten); Putchar (A%Ten+'0');}Const intn=1000005;//Code begin ...inttrie[205][ -], top;CharStr[n], s[ the];BOOLVis[n];voidInit () {top=1; MEM (trie[0],0);}voidInsChar*s) {    intRT, NXT;  for(rt=0; *s; RT=NXT, + +s) {NXT=trie[rt][*s-'a']; if(!NXT) mem (Trie[top],0), trie[rt][*s-'a']=nxt=top++; } trie[rt][ -]=1;}voidFindintLintR) {    intRT, NXT, I;  for(rt=0, I=l; i<=r; RT=NXT, + +i) {NXT=trie[rt][str[i]-'a']; if(!NXT)return ; if(trie[nxt][ -]) vis[i]=true; }}intMain () {intN, M; scanf ("%d%d",&n,&m);    Init (); For (I,1, N) scanf ("%s", s+1), INS (s+1); For (I,1, M) {scanf ("%s", str+1); intLen=strlen (str+1); Mem (Vis,0); vis[0]=true; intans; For (J,0, Len)if(Vis[j]) find (j+1>len?len:j+1, j+Ten>len?len:j+Ten), ans=J; printf ("%d\n", ans); }    return 0;}
View Code

Bzoj 1212 L language (dp+ dictionary tree)

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.