Uvalive 5103 Computer Virus on Planet Pandora Description the number of species that appear in the pattern string AC automata

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

Case number

N Mode string

A parent string.

Q: The number of occurrences of a pattern string (a pattern string appears only once)

For "ABC", if the mother string appeared "CBA" Such crossdress, also appeared.

So:

1

Abc

Cba

Ans = 1

#include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <algorithm >using namespace Std;const int maxnode = 250*1000+10000;const int sigma_size = 26;struct Trie{int ch[maxnode][sigma_siz E];int Val[maxnode]; The number of occurrences of the word in the pattern string int last[maxnode];int F[maxnode]; Mismatch array int Num[maxnode];    The number of times the word appears in the text string int pre[maxnode];    The precursor of the word int len[maxnode]; The word length at the end of the word int char[maxnode];   The word corresponds to the letter int Road[maxnode];    Path compression optimization The number of occurrences of the calculation pattern string int sz;int Newnode () {Val[sz] = F[sz] = Last[sz] = Len[sz] = Num[sz] = 0;    memset (Ch[sz], 0, sizeof Ch[sz]);    return sz++;    }void init () {sz=0; Newnode ();} int idx (char c) {return C ' A ';}  int Insert (char *s) {int u = 0;for (int i = 0, C; s[i]; i++) {c = idx (S[i]), if (!ch[u][c]) ch[u][c] = Newnode ();p re[ch[u][c]] = U CHAR[CH[U][C]] = s[i];len[ch[u][c]] = len[u]+1;road[ch[u][c]] = 1;u = Ch[u][c];}   Val[u] = 1;num[u] = 0;return u;}        void Getfail () {queue<int> q; for (int i = 0; i<sigma_size; i++) if (Ch[0][i]) Q.push (Ch[0][i]);        int r, c, u, v;            while (!q.empty ()) {R = Q.front (); Q.pop ();                for (c = 0; c<sigma_size; C + +) {u = ch[r][c];                if (!u) continue;                Q.push (U);                v = f[r]; while (v && ch[v][c] = = 0) v = f[v];            Go up along the mismatch edge if there is a node after mismatch and its child node C exists then end loop f[u] = Ch[v][c]; }}}void find (char *t) {//The number of occurrences of the calculation pattern string: (multiple occurrences per type) int j = 0;for (int i = 0, C, temp; T[i];                 i++) {c = idx (T[i]), while (J && ch[j][c]==0) j = f[j];j = Ch[j][c];temp = J;while (temp) {num[temp]++;            temp = f[temp]; }}}} void Find_kind (char *t, int &ans) {//calculates the number of species, repeated occurrences are no longer computed (if multiple queries are to be added here for (i=0->sz) Lu[i]=1;int j = 0 , I, C, temp;for (i = 0; T[i]; i++) {c = idx (T[i]), while (J && ch[j][c] = = 0) j = f[j];j = Ch[j][c];temp = J;while (temp && road[temp]) {if (V Al[temp]) {++ans;vaL[temp] = 0;}                Road[temp] = 0;            temp = f[temp]; }}}}ac;char s[1015], a[5100010], b[5100010], C;int N, num;int main () {int T; scanf ("%d", &t); while (T--&gt ; 0) {ac.init (); scanf ("%d", &n), while (n--) {scanf ("%s", s); Ac.insert (s);} Ac.getfail (); int top = 0;SCANF ("%s", a), int i = 0;while (A[i]) {if (a[i]!= ' [') b[top++] = a[i];else {num = 0;i++;while (' 0 ' &L t;= A[i] && a[i] <= ' 9 ') num = num*10 + a[i]-' 0 ', i++;c = a[i];i++;while (num--> 0) {b[top++] = c;}} i++;} A[top] = B[top] = 0;for (int i = top-1; I >= 0; i--) a[i] = b[top-i-1];int ans = 0;ac.find_kind (b, ans); Ac.find_kind (A, a NS);p rintf ("%d\n", ans);} return 0;}


Uvalive 5103 Computer Virus on Planet Pandora Description the number of species that appear in the pattern string AC automata

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.