"bzoj4327" JSOI2012 Xuanwu password AC automatic machine

Source: Internet
Author: User

Title Description

In the beautiful Xuanwu Lake, the chicken-ming temple side, the Coop mountain, there is a fertile and beautiful land, people call 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?

Input

The first line has two integers, N and M, which represent 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.

Output

The output has 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 input

9}
Snnssns
Nnss
NNN
Wsee

Sample output

4
2
0

Exercises

AC Automatic Machine

First, all the pattern strings are added to the trie, and the fail pointer and the Trie diagram are constructed.

Open a bool array and record whether each location can be reached.

Then the matching string runs through the tries graph and runs to a certain position, then the bool corresponding to this position is assigned true.

Also, because fail is the suffix of the current position, the bool of fail should be assigned true.

This loop is carried out until a location is already true.

This method can guarantee the time complexity of O (n+ml).

Finally, the location of a string is assigned to true.

#include <cstdio> #include <cstring> #include <queue> #define N 10000010using namespace std;queue< Int> Q;int next[n][4], fail[n], len[n], tot = 1, pos[100010][110];bool Vis[n];char str[n], w[n];int tra (char ch) {R Eturn ch = = ' E '? 0:ch = = ' S '? 1:ch = = ' W '? 2:3;} void build () {int x, i;for (i = 0; i < 4; i + +) next[0][i] = 1;q.push (1); while (!q.empty ()) {x = Q.front (), Q.pop (); fo R (i = 0; i < 4; i + +) {if (Next[x][i]) fail[next[x][i]] = Next[fail[x]][i], Q.push (Next[x][i]); else next[x][i] = Next [Fail[x]] [i];}}}  int main () {int n, m, I, J, t;scanf ("%d%d%s", &n, &m, str + 1); for (i = 1; I <= m; i + +) {scanf ("%s", W + 1), len[i] = strlen (w + 1), for (j = t = 1; J <= Len[i]; j + +) {if (!next[t][tra (W[j])]) Next[t][tra (w[j]) = ++tot; t = Next[t][tra (W[j])], pos[i][j] = t;}} Build (); vis[1] = 1, t = 1;for (i = 1; I <= n; i + +) {t = Next[t][tra (Str[i])];for (j = t;!vis[j]; j = fail[j]) vis[ j] = 1;} for (i = 1; I <= m; i + +){for (j = len[i]; j; J--) if (Vis[pos[i][j]]) break;printf ("%d\n", j);} return 0;}

"bzoj4327" JSOI2012 Xuanwu password AC automatic machine

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.