"AC automaton" bzoj4327:jsoi2012 Xuanwu Password

Source: Internet
Author: User

There is no talk about the topic, mainly the details and experience of the problem.

Description in the beautiful Xuanwu Lake, chicken-ming temple side, Coop Mountain front, there is a rich and beautiful land, people called into Xianghe. Legend has it that one day, a wisp of purple gas from the sky, only a moment disappeared in the pilgrimage river. The old man said it was the basaltic spirit who hid the heavenly book here. Many years later, people finally found the text with the Xuanwu code in the Xianghe area. More amazing is that the text with the Xuanwu code, and Xuanwu Lake on the South bank Taicheng structure has a subtle connection. So, the long deciphering work began. After analysis, we can use four directions to describe the Taicheng City brick layout, may wish to use a length of N sequence to describe, the sequence of elements are ' E ', ' S ', ' W ', ' N ', representing the cardinal and the four, we call the mother string. And the mysterious Xuanwu code is a four-elephant pattern described by the M-paragraph text. Here the four elephants, respectively, is the east of the Dragon, west of the White Tiger, the south of the Rose Finch, the north of the Xuanwu, to the Cardinal four to the opposite. Now, archaeologists are confronted with a difficult problem. For each piece of text, what is the maximum match length of the prefix on the parent string? The first line of input has two integers, N and m, representing the length of the parent string and the number of text segments. The second line is a string of length n, and all characters satisfy one of the E,S,W and N. After M lines, each line has a string that describes a text with a Xuanwu password. Still satisfied, all characters are satisfied with one of the E,s,w and N. The output outputs have m lines, corresponding to the M-segment text. Each row outputs a number that represents the prefix of this paragraph of text with the maximum matching string length of the parent string. Sample Input7 3
Snnssns
Nnss
NNN
WseeSample Output4
2
0HINT

For 100% of the data, n<=10^7,m<=10^5, the length of each piece of text <=100.

Problem analysis

The problem is obvious: mark which nodes have been matched, then DFS to find the maximum depth that has been matched.

Here is a record of the experience of doing the problem:

    1. The character set can compress as much as possible (and discretization is one reason), trie after all | The constant of the s| is here.
    2. This optimization is very important when querying for a jump match (int v=u; v&&!vis[v]; v=nxt[v]) .

Above.

1#include <bits/stdc++.h>2 Const intMaxnode =10000035;3 Const intMAXN =10000035;4 5 Chars[maxn],t[103];6 intn,m,c[100035],l[100035],cnt;7 structAcautomaton8 {9     BOOLVis[maxnode];Tenstd::queue<int>Q; One     intnxt[maxnode],f[maxnode][5],fa[maxnode],tot; A      -Inlineint Get(Charch) -     { the         if(ch=='W')return 0; -         if(ch=='E')return 1; -         if(ch=='S')return 2; -         return 3; +     } -     voidInsertChar*s) +     { A         intU =1, Lens =strlen (s); at          for(intI=0; i<lens; i++) -         { -             intc =Get(S[i]); -             if(!f[u][c]) f[u][c] = ++tot, fa[f[u][c]] =u; -U =F[u][c]; -         } inC[++CNT] =u; -     } to     voidbuild () +     { -          for(intI=0; i<4; i++) f[0][i] =1; theQ.push (1); *          while(Q.size ()) $         {Panax Notoginseng             inttt =Q.front (); - Q.pop (); the              for(intI=0; i<4; i++) +                 if(F[tt][i]) ANxt[f[tt][i]] =F[nxt[tt]][i], Q.push (F[tt][i]); the                 ElseF[tt][i] =F[nxt[tt]][i]; +         } -     } $     intQueryintSintt) $     { -          for(; s; s=fa[s], t--) -             if(Vis[s])returnT; the         return 0; -     }Wuyi     voidMatchChar*s) the     { -         intU =1, Lens =strlen (s); Wu          for(intI=0; i<lens; i++) -         { About             intc =Get(S[i]); $U =F[u][c]; -              for(intV=u; v&&!vis[v]; V=NXT[V]) Vis[v] =1; -         } -     } A }f; +  the intMain () - { $scanf"%d%d%s",&n,&m,s); theF.tot =1; the      for(intI=1; i<=m; i++) the     { thescanf"%s", T); - F.insert (t); inL[i] =strlen (t); the     } the f.build (), F.match (s); About      for(intI=1; i<=m; i++) theprintf"%d\n", F.query (C[i], l[i])); the     return 0; the}

END

"AC automaton" bzoj4327:jsoi2012 Xuanwu Password

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.