zoukankan      html  css  js  c++  java
  • JSOI2007 文本生成器

    题目链接:戳我

    非常不好意思,因为想要排版,所以今天先只把代码贴出来,题解明天补。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #define MAXN 6010
    #define mod 10007
    using namespace std;
    int n,m,tot;
    int dp[MAXN][MAXN];
    char ss[MAXN];
    struct Node{int ch[26],fail,end;}t[MAXN<<1];
    inline int fpow(int x,int y)
    {
        int cur_ans=1;
        while(y)
        {
            if(y&1) cur_ans=1ll*cur_ans*x%mod;
            x=1ll*x*x%mod;
            y>>=1;
        }
        return cur_ans;
    }
    inline void build(char *s)
    {
        int len=strlen(s+1),x=0;
        for(int i=1;i<=len;i++)
        {
            if(!t[x].ch[s[i]-'A']) t[x].ch[s[i]-'A']=++tot;
            x=t[x].ch[s[i]-'A'];
        }
        t[x].end=1;
    }
    inline void get_fail()
    {
        queue<int>q;
        int x=0;
        for(int i=0;i<26;i++)
            if(t[x].ch[i])
                t[t[x].ch[i]].fail=0,q.push(t[x].ch[i]);
        while(!q.empty())
        {
            int u=q.front();q.pop();
            t[u].end|=t[t[u].fail].end;
            for(int i=0;i<26;i++)
            {
                if(t[u].ch[i]) t[t[u].ch[i]].fail=t[t[u].fail].ch[i],q.push(t[u].ch[i]);
                else t[u].ch[i]=t[t[u].fail].ch[i];
            }
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("ce.in","r",stdin);
        #endif
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",ss+1);
            build(ss);
        }
        get_fail();
        dp[0][0]=1;
        int ans=fpow(26,m)%mod,cur_ans=0;
        for(int i=1;i<=m;i++)
            for(int j=0;j<=tot;j++)
                for(int k=0;k<26;k++)
                {
                    // cout<<t[j].ch[k]<<endl;
                    if(t[t[j].ch[k]].end==0)
                    {
                        dp[t[j].ch[k]][i]=(dp[t[j].ch[k]][i]+dp[j][i-1])%mod;
                        // printf("%d
    ",dp[t[j].ch[k]][i]);
                    }
                }
        for(int i=0;i<=tot;i++) cur_ans=(cur_ans+dp[i][m])%mod;
        // printf("cur_ans=%d
    ",cur_ans);
        printf("%d
    ",(ans+mod-cur_ans)%mod);
        return 0;
    }
    
  • 相关阅读:
    写在前面
    你应该知道的 RPC 原理
    虚函数 继承 多态
    指针 函数指针 指针数组
    Python使用RMF聚类分析客户价值
    数据挖掘关联分析中的支持度、置信度和提升度
    OpenCV2.4.4+Cmake2.8+Vs2010编译createsamples+traincascade程序用来训练样本
    UNITY + OpenCVSharp调节图像对比度
    使用单精度类型变量
    有符号基本整型
  • 原文地址:https://www.cnblogs.com/fengxunling/p/10884282.html
Copyright © 2011-2022 走看看