Bzoj 1030: [JSOI2007] Text generator (DP on AC automaton)

Source: Internet
Author: User

1030: [JSOI2007] Text generator time limit: 1 Sec Memory Limit: 162 MB
Submit: 2635 Solved: 1090
[Submit] [Status] [Discuss] Description

Jsoi to the players zyx a task, the production of a "text generator" computer software: The user of the software is some low-young people, they are now using the GW Text Generator V6 version. The software can generate some random articles----always generate a fixed length and completely random article--that is, every byte in the generated article is completely random. If an article contains at least one word that the user understands, then we say this article is readable (we call the article A contains the word B when and only if the word b is a substring of article a). However, even by this standard, the article generated by the V6 version of the GW text generator used by the consumer is also almost completely unreadable . ZYX need to indicate the number of readable text in all the text generated by the GW text Generator V6 so that the V7 update can be successfully obtained. Can you help him?

Input

The first line of the input file contains two positive integers, the total number of words that the user understands (<=), and the GW text generator v6 The resulting text fixed length m; The following n lines, each containing a word that the user understands. All words and text will not be longer than 100 and may contain only capital letters a. Z.

Output

An integer that represents the total number of possible articles. You only need to know the value of the result modulus 10007.

Sample Input2 2
A
BSample Output -

DP on AC automaton
DP[0][I][J] Indicates the number of strings that do not meet the conditions in the J state to the first I bit
DP[1][I][J] Indicates the number of strings that meet the condition at the end of the first position

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 6005;const int MOD = 10007;int Que[n], FR, ta;struct ACM {int cnt;int nxt[n][26], sum[n], fail[n];void i NIT () {for (int i = 1; I <= cnt; ++i) {sum[i] = fail[i] = 0;for (int j = 0; J <; ++j) nxt[i][j] = 0;} CNT = 1;for (int i = 0; i <; ++i) nxt[0][i] = 1;} void Insert (String str) {int now = 1;int len = str.length (), for (int i = 0; i < len; ++i) {if (Nxt[now][str[i]-' A '] = = 0) Nxt[now][str[i]-' a '] = ++cnt;now = nxt[now][str[i]-' a '];} Sum[now] = 1;} void Build_fail () {fr = Ta = 0;que[ta++] = 1;fail[1] = 0;while (fr! = TA) {int now = que[fr++];for (int i = 0; i < 26; + +) i) {int x = nxt[now][i];if (x = = 0) continue;int tmp = Fail[now];while (nxt[tmp][i] = = 0) tmp = fail[tmp];fail[x] = nxt[tmp] [i];que[ta++] = x;}}} void Debug () {for (int i = 1; I <= cnt, ++i) {cout<<i<< ":" << "fail =" <<fail[i]<< "["; for (int j = 0; J &LT 26; ++J) {if (Nxt[i][j]) Cout<<char (j + ' A ') << ', ' <<nxt[i][j]<< ';} Puts ("]");}} void Gao (int, int);} Acm;int dp[2][105][n];void Acm::gao (int N, int m) {build_fail ();//debug () memset (DP, 0, sizeof (DP));DP [0][0][1] = 1;for ( int i = 1; I <= m; ++i) {for (int j = 1; j <= cnt; ++j) {int u = j;for (int k = 0; k <; ++k) {int v = u;while (nxt[v][k] = = 0) v = fail [V];V = Nxt[v][k];if (Sum[v]) {Dp[1][i][v] = (Dp[1][i][v] + dp[1][i-1][u] + dp[0][i-1][u])% mod;dp[0][i][v] + = 0;} els e {Dp[1][i][v] = (Dp[1][i][v] + dp[1][i-1][u])% mod;dp[0][i][v] = (Dp[0][i][v] + dp[0][i-1][u])% MOD;}}} int ans = 0;for (int i = 1; I <= cnt; ++i) ans = (ans + dp[1][m][i])% Mod;cout<<ans<<endl;}  String Str[65];bool ok (int x, int n) {int len = str[x].length (); for (int i = 0; i < n; ++i) {if (i = = x) continue;int pos = Str[x].find (Str[i]); if (pos >= 0 && Pos < len) return false;} return true;} int main () {int n, m;cin>>n>>m;acm.init ();t i = 0; I < n; ++i) {cin>>str[i];} Sort (str, str + n), n = unique (str, str + N)-str;for (int i = 0; i < n; ++i) {if (ok (i, n)) Acm.insert (Str[i]);} Acm.gao (n, m); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 1030: [JSOI2007] Text generator (DP on AC automaton)

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.