zoukankan      html  css  js  c++  java
  • BZOJ 1030 [JSOI2007]文本生成器 (AC自动机 + DP)

    1030: [JSOI2007]文本生成器

    Time Limit: 1 Sec  Memory Limit: 162 MB
    Submit: 5225  Solved: 2158
    [Submit][Status][Discuss]

    Description

      JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,
    他们现在使用的是GW文本生成器v6版。该软件可以随机生成一些文章―――总是生成一篇长度固定且完全随机的文
    章—— 也就是说,生成的文章中每个字节都是完全随机的。如果一篇文章中至少包含使用者们了解的一个单词,
    那么我们说这篇文章是可读的(我们称文章a包含单词b,当且仅当单词b是文章a的子串)。但是,即使按照这样的
    标准,使用者现在使用的GW文本生成器v6版所生成的文章也是几乎完全不可读的?。ZYX需要指出GW文本生成器 v6
    生成的所有文本中可读文本的数量,以便能够成功获得v7更新版。你能帮助他吗?

    Input

      输入文件的第一行包含两个正整数,分别是使用者了解的单词总数N (<= 60),GW文本生成器 v6生成的文本固
    定长度M;以下N行,每一行包含一个使用者了解的单词。这里所有单词及文本的长度不会超过100,并且只可能包
    含英文大写字母A..Z

    Output

      一个整数,表示可能的文章总数。只需要知道结果模10007的值。

    Sample Input

    2 2
    A
    B

    Sample Output

    100

    HINT

     

    Source

    析:直接不好求,可以反过来求,求生成的文本不包含任何一个串,这样就可以用AC自动机进行求了,把所有的文本串都插入到AC自动机里去,然后DP,在转移的时候不要走已经标记的结点就好。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #define debug() puts("++++")
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    //#define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 150000 + 10;
    const int maxm = 3e5 + 10;
    const int mod = 10007;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, -1, 0, 1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    const int maxnode = 120 * 60 + 10;
    const int sigma = 26;
    
    struct Aho{
      int ch[maxnode][sigma], f[maxnode];
      bool val[maxnode];
      int sz;
    
      void init(){ sz = 1;  ms(ch[0], 0); }
      inline int idx(char ch){ return ch - 'A'; }
    
      void insert(const char *s){
        int u = 0;
        for(int i = 0; s[i]; ++i){
          int c = idx(s[i]);
          if(!ch[u][c]){
            ms(ch[sz], 0);
            val[sz] = 0;
            ch[u][c] = sz++;
          }
          u = ch[u][c];
        }
        val[u] = 1;
      }
    
      void getFail(){
        queue<int> q;  f[0] = 0;
        for(int c = 0; c < sigma; ++c){
          int u = ch[0][c];
          if(u){ f[u] = 0;  q.push(u); }
        }
    
        while(!q.empty()){
          int r = q.front();  q.pop();
          for(int c = 0; c < sigma; ++c){
            int u = ch[r][c];
            if(!u){ ch[r][c] = ch[f[r]][c];  continue; }
            q.push(u);
            int v = f[r];
            while(v && !ch[v][c])  v = f[v];
            f[u] = ch[v][c];
            val[u] |= val[f[u]];
          }
        }
      }
    
      int dp[110][maxnode];
    
      int solve(int n){
        ms(dp, 0);  dp[0][0] = 1;
        for(int i = 0; i < n; ++i)
          for(int j = 0; j < sz; ++j){
            if(!dp[i][j])  continue;
            for(int k = 0; k < sigma; ++k){
              int nxt = ch[j][k];
              if(val[nxt])  continue;
              dp[i+1][nxt] = (dp[i+1][nxt] + dp[i][j]) % mod;
            }
          }
        int ans = 0;
        for(int i = 0; i < sz; ++i)  ans = (ans + dp[n][i]) % mod;
        return ans;
      }
    };
    Aho aho;
    
    char s[120];
    
    int main(){
      int kase = 0;
      scanf("%d %d", &m, &n);
      aho.init();
      while(m--){
        scanf("%s", s);
        aho.insert(s);
      }
      aho.getFail();
      int ans = 1;
      for(int i = 0; i < n; ++i)  ans = ans * 26 % mod;
      printf("%d
    ", (ans - aho.solve(n) + mod) % mod);
      return 0;
    }
    

      

  • 相关阅读:
    搜广推04-信息检索任务&数据集&LeadBoard&评价指标
    搜广推&NLP03-顶会track记录
    搜广推02-DeepMatch 模型总结[SIGIR2019 tutorial]
    搜广推01-信息检索领域大佬总结
    计算机基础01-终端命令行、VIM、git、CICD
    【python】彼岸图网4K壁纸批量爬虫共1.48G(多线程/多进程)
    【python】不到500行代码实现flappybird小游戏
    解决pyinstaller打包程序太大的问题
    解决pipenv install报错FileNotFoundError: [Errno 2] No such file or directory: ‘d:\miniconda3\Lib\venv
    【python】如何将matplotlib的标题置于图片下方
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7843933.html
Copyright © 2011-2022 走看看