"Bzoj 1030" "Jsoi 2007" text generator ac automata + recursive

Source: Internet
Author: User

Always do not understand how to do ah, think for a long time $twt$

Finally understand why find the first $fail$ to meet the criteria to calculate, because avoid repetition, this answer,,,

Then $root$ below to connect to 26 nodes, here 26 letters are not in the dictionary is replaced with $f[i][0]$, this also thought for a long time, $==$ I still go Home farm.

#include <cstdio> #include <cstring> #include <algorithm> #define MO 10007#define N 103#define M 6003using namespace Std;int q[m], c[m][26], w[m], fail[m], f[n][m], N, M, cnt = 0;inline void ins (char *s) {int len = Strl En (s), now = 0;for (int i = 0; i < len; ++i) {int t = s[i]-' A '; if (!c[now][t]) c[now][t] = ++cnt;now = C[now][t];} W[now] = 1;} inline void BFS () {int head = 0, tail = 1, now = 0;q[1] = 0;while (head! = tail) {now = q[++head];for (int t = 0; t < 26 ; ++t) if (C[now][t]) {Q[++tail] = c[now][t];if (now = = 0) continue;int tmp = fail[now];while (tmp &&!c[tmp][t]) tmp = Fail[tmp];fail[c[now][t]] = C[tmp][t];if (W[c[tmp][t]]) w[c[now][t]] = 1; //!!!!!!!}}} inline int Ipow (int x, int nn) {int ans = 1;for (int t = x; nn; nn >>= 1, t = (t * t)% mo) if (NN & 1) {ans = (an S * t); if (ans >= mo) ans = ans% mo;} return ans;}  inline void AC () {f[0][0] = 1;for (int i = 0; I <= m; ++i) for (int j = 0; J <= CNT, ++j) if (F[i][j]) {for (int t = 0; t < 26; ++T) {int tmp = J;WHILE (tmp &&!c[tmp][t]) tmp = FAIL[TMP];TMP = C[tmp][t];if (!w[tmp]) {f[i + 1][tmp] + = F[i][j];i F (f[i + 1][tmp] >= mo) f[i + 1][tmp]%= mo;}}} int _ = 0, Al = Ipow (+, M), for (int i = 0; I <= cnt; ++i) if (!w[i]) {_ + = F[m][i];if (_ > Mo) _%= mo;} int ans = ((al-_)% mo + mo)%mo;printf ("%d\n", ans); int main () {scanf ("%d%d", &n, &m), char s[n];for (int i = 1; I <= n; ++i) scanf ("%s", s), ins (s); BFS (); AC (); return 0;}

Finally made it out, because looking at someone else's template, which also,,,

"Bzoj 1030" "Jsoi 2007" text generator ac automata + recursive

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.