• Bzoj1030:[JSOI2007]文本生成器


    题面

    Bzoj

    Sol

    (AC)自动机上(DP)
    总数(-)不合法

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(6005);
    const int Zsy(10007);
    
    IL ll Input(){
        RG ll x = 0, z = 1; RG char c = getchar();
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        return x * z;
    }
    
    int n, m, ans = 1, ch[26][_], fail[_], ed[_], tot, f[500][_];
    queue <int> Q;
    char s[_];
    
    IL void Insert(){
    	RG int l = strlen(s), x = 0;
    	for(RG int i = 0; i < l; ++i){
    		RG int id = s[i] - 'A';
    		if(!ch[id][x]) ch[id][x] = ++tot;
    		x = ch[id][x];
    	}
    	ed[x] = 1;
    }
    
    IL void BuildFail(){
    	for(RG int i = 0; i < 26; ++i) if(ch[i][0]) Q.push(ch[i][0]);
    	while(!Q.empty()){
    		RG int u = Q.front(); Q.pop();
    		for(RG int i = 0; i < 26; ++i)
    			if(!ch[i][u]) ch[i][u] = ch[i][fail[u]];
    			else fail[ch[i][u]] = ch[i][fail[u]], Q.push(ch[i][u]);
    		ed[u] |= ed[fail[u]];
    	}
    }
    
    IL void Chk1(RG int &x, RG int y){
    	x += y;
    	if(x >= Zsy) x -= Zsy;
    }
    
    IL void Chk2(RG int &x, RG int y){
    	x -= y;
    	if(x < 0) x += Zsy;
    }
    
    int main(RG int argc, RG char* argv[]){
        n = Input(), m = Input();
    	for(RG int i = 1; i <= m; ++i) ans = ans * 26 % Zsy;
    	for(RG int i = 1; i <= n; ++i) scanf(" %s", s), Insert();
    	BuildFail(), f[0][0] = 1;
    	for(RG int i = 0; i < m; ++i)
    		for(RG int j = 0; j <= tot; ++j){
    			if(!f[i][j] || ed[j]) continue;
    			for(RG int k = 0; k < 26; ++k)
    				if(!ed[ch[k][j]]) Chk1(f[i + 1][ch[k][j]], f[i][j]);
    		}
    	for(RG int i = 0; i <= tot; ++i) if(!ed[i]) Chk2(ans, f[m][i]);
    	printf("%d
    ", ans);
        return 0;
    }
    
    
  • 相关阅读:
    css3——box-sizing属性
    HTML5存储--离线存储
    微信公众号爆出前端安全漏洞
    Js获取宽高度的归纳集锦总结
    Yii 2 修改 URL 模式为 PATH 模式,并隐藏index.php
    SQL 查询优化 索引优化
    linux提示语言包
    安装linux工作环境
    linux常用命令
    PHP解决抢购、秒杀、抢楼、抽奖等阻塞式高并发库存防控超量的思路方法
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8438699.html
Copyright © 2020-2023  润新知