[Bzoj 3530] [Sdoi2014] Number of "AC automata +DP"

Source: Internet
Author: User

Title Link: BZOJ-3530

Problem analysis

It is obviously AC automata +DP, plus digital statistics.

WZY God Ben the choice of conscience, but last year I was too weak. Much weaker than it is now.

In fact, now do this problem, I did not think of a complete solution.

I came up with an O (l^3) approach:

According to the idea of digital statistics, the number of legal types of the number of the length of Len is counted first, the enumeration begins, and then the AC automata DP, using f[i][j] to indicate the number of the number of legal digits on the J-node. This is O (l^2).

Then the length is equal to the part of N, just according to the digit statistic, a bit is pushed backwards, then each o (l^2) DP is asked, so the total complexity is O (l^3).

Note Can not walk the node containing the pattern string, first Trie the node of the pattern string must not go, second, if the Fail chain along a node has been up, as long as the chain has a node is not allowed to go, then this node can not go.

So you need to start from each node, against the side of the Fail DFS, if the current state will pass through the point all Ban off. But the data seems to be weaker, and this is not going to go wrong.

But this would be a mistake: for example, there are 123, 2 of these two strings, and then it along the 1 and 2 has been backwards matching, there is no matching success 123, but in fact already contains 2 this string.

This complexity is only 70 points.

The complexity of the positive solution is O (l^2): for numbers with insufficient Len length, or Direct, O (l^2).

For numbers that are equal to Len, we use an O (l^2) DP to find out, we add the state to one dimension, G[i][j][k], the first two bits are the first bit, go to the J node, K is a [0/1] variable, indicating whether the state is coming along each of the N.

So we know that if the current state is coming in accordance with each of N, then the next step is to transfer [0, n[i+1]] these numbers. Otherwise, all the numbers can be transferred. This will find all the valid numbers that are less than or equal to N.

The total complexity is O (l^2).

The general practice of AC automata +DP: f[i][j] means walk I step, go to the J node, sometimes need to add a 3rd dimension or more.

Then the DP is enumerated I, enumeration J, and the enumeration is transferred to the next character.

Code
#include <iostream> #include <cstdlib> #include <cstring> #include <cmath> #include <  algorithm> #include <cstdio> #include <queue>using namespace std;const int MaxL = 5, Maxnode = 15000 + 5, Mod = 1000000007;int len, l, M, Index, Ans;int F[maxl][maxnode], G[maxl][maxnode][2];char S[maxl], ns[maxl];struct Tri E{int x, Idx;bool Ban; Trie *fail, *child[10];} Ta[maxnode], *p = TA, *root, *zero; trie* NewNode (int Num = 0) {++p; P-x = Num; P--IDX = ++index; P-Fail = NULL; P---Ban = False;memset (P-, child, 0, sizeof (P-and child)); return p;} void Insert (char *s, int l) {int t; Trie *now = root;for (int i = 1; I <= l; ++i) {t = s[i]-' 0 '; if (now-and child[t] = = NULL) Now--child[t] = Newnod E (t); now = Child[t];} Now-and Ban = true;} struct Edge{int v; Edge *next;} E[maxnode], *pe = E, *point[maxnode];inline void Addedge (int x, int y) {++pe; PE-v = y; PE, Next = point[x]; POINT[X] = PE;} BOOL Visit[maxnode];void DFS (int x, bool f) {visit[x] = true;ta[x]. Ban = (Ta[x]. Ban) | | F;for (Edge *j = point[x]; j; j = J-Next) DFS (J-V, Ta[x]. Ban);} Queue<trie *> q;void Build_fail () {while (! Q.empty ()) Q.pop (); Q.push (Root); Trie *now;while (! Q.empty ()) {now = Q.front (); Q.pop ();  Addedge (now---Fail, IDX, now-and IDX); for (int i = 0; I <= 9; ++i) {if (now--child[i] = = NULL) Now-- Child[i] = now-and fail-child[i];else {now-child[i]-fail = now--fail-and child[i]; Q.push (now--child[i]);}} for (int i = 1; I <= Index; ++i) if (! Visit[i]) DFS (i, ta[i]. Ban);} void Usual_disco () {memset (f, 0, sizeof (f)), for (int i = 1; I <= 9; ++i) if (Root, child[i], Ban = = False) ++f[1 ][root, Child[i], idx];for (int i = 1; i < Len; ++i) for (int j = 1; j <= Index; ++j) {if (Ta[j]. Ban) continue; Ans = (ans + f[i][j])% mod;for (int t = 0; t <= 9; ++t) if (!ta[j]. Child[t] (Ban) {f[i + 1][ta[j]. CHILD[T] + Idx] + = F[i][j];f[i + 1][ta[j]. ChIld[t], IDX]%= Mod;}} memset (g, 0, sizeof (g)); for (int i = 1; i < ns[1]-' 0 '; ++i) if (Root-child[i], Ban = = False) ++g[1][root-&G T Child[i]-idx][0];if (Root-child[ns[1]-' 0 ', Ban = = False) ++g[1][root, Child[ns[1]-' 0 ', IDX ][1];for (int i = 1; I <= len; ++i) for (int j = 1; j <= Index; ++j) {if (Ta[j]. Ban) continue;if (i = = Len) {ans = (ans + g[i][j][0])% Mod; Ans = (ans + g[i][j][1])% Mod;continue;} for (int t = 0; t <= 9; ++t) if (!ta[j]. Child[t] (Ban) {g[i + 1][ta[j]. Child[t], idx][0] + + g[i][j][0];g[i + 1][ta[j]. CHILD[T]-idx][0]%= Mod;} for (int t = 0; t < Ns[i + 1]-' 0 '; ++t) if (!ta[j]. Child[t] (Ban) {g[i + 1][ta[j]. Child[t], idx][0] + + g[i][j][1];g[i + 1][ta[j]. CHILD[T]-idx][0]%= Mod;} if (!ta[j]. Child[ns[i + 1]-' 0 '] (Ban) {g[i + 1][ta[j]. Child[ns[i + 1]-' 0 ', idx][1] + + g[i][j][1];g[i + 1][ta[j]. Child[ns[i + 1]-' 0 ', idx][1]%= Mod;}} int main () {scanf ("%s", Ns + 1); Len= strlen (Ns + 1); scanf ("%d", &m); Root = NewNode (); Zero = NewNode (); Root-Fail = zero;for (int i = 0; I <= 9; ++i) Zero-child[i] = root;for (int i = 1; I <= m; ++i) {scanf ("% S ", S + 1); L = strlen (s + 1); Insert (S, l);} Build_fail (); Usual_disco (); Ans%= mod;cout << ans << endl;return 0;}

  

[Bzoj 3530] [Sdoi2014] Number of "AC automata +DP"

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.